Render drupal field widget in custom page - php

I have my module called mymodule.
In mymodule.module I have:
function mymodule_menu() {
$items['mymodule/ship/%node'] = array(
'title' => t('Shipment details'),
'page callback' => '_mymodule_addr',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'weight' => 0,
);
return $items;
}
I want to render the addressfield widget inside the page. Then I want to read form values.
Can you help me?

What about using the Field API:
Within your callback function use:
$node = node_load($parameter);
$my_field_value = '<front>';
$my_field_items = field_get_items('node', $node, 'field_my_field');
if ($my_field_items) {
$my_field_first_item = reset($my_field_items);
$my_field_value = $my_field_first_item['value'];
}
return l($my_field_value, 'Link name');
Have a look at: Drupal Field API and find the right function for you.
Also at: Drupal Examples are some useful examples that you might use.
As least, don't forget that you can use a custom .tpl file in your theme where you put custom markup to your field.

Related

Error - "This version is not compatible with Drupal 7.x and should be replaced."

I am trying to create and enable a module in Drupal 7.59 and I am getting below issue on admin page.
This version is not compatible with Drupal 7.x and should be replaced.
The module name is "events" and the .info file content is
name = Events Creation
description = This module will create media events
core = 7.59
And events module code is
<?php
function events_menu() {
$items = array();
$items['test/events'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Event Creation Form', //page title
'description' => 'A form to create an event.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('events_form'), //put the name of the form here
'access callback' => TRUE
);
return $items;
}
function events_form($form, &$form_state) {
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
return $form;
}
function events_form_validate($form, &$form_state) {
}
function events_form_submit($form, &$form_state) {
}
Any help is highly appreciated. Thanks in advance.
Sorry my mistake. It would be core = 7.x

Theme not working for hook_menu page

I have created a simple module with a hook_menu
function course_list_menu() {
$items['course-list'] = array(
'title' => 'Example Page',
'page callback' => 'course_list_page',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function course_list_page() {
print '<h1>WILL BE UP SOON </h1>';
print '<h3>this page is getting build<h3>';
}
As i saw in examples on youtube and some other sites, this text should come in the content region when i visit the link ( with header and footer ). But in my case it is coming in a blank page
Am i missing some thing ?
How can i display this content int the content region.
My current output is like http://prntscr.com/bpff9q
using bootstrap theme
You need to return something that Drupal can work with and apply to the template files.
Try this:
function course_list_page() {
return array('#markup' => '<h1>WILL BE UP SOON </h1><h3>this page is getting build<h3>');
}

Assist with Drupal Admin Blank page for custom functions or code

I am trying to learn Drupal. What I want to do is create a backend page (that is on main menu) where I can run my own functions and code. I have been doing research and found out that to do this I need to create a module. And if I run the "hook_menu" function - i can get that backend page to be on the menu. I found code for a drupal module that does this, and it loads a form for a "config settings" page. Here is the code:
function add_game_menu() {
$items = array();
$items['admin/add_game'] = array(
'title' => 'Add Gm Pg',
'description' => 'Description of your add game page',
'page callback' => 'drupal_get_form',
'page arguments' => array('add_game_admin'),
'access arguments' => array('administer add_game settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function add_game_admin() {
$form = array();
$form['add_game_maxdisp'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of links'),
'#default_value' => variable_get('add_game_maxdisp', 3),
'#size' => 2,
'#maxlength' => 2,
'#description' => t("The maximum number of links to display in the block."),
'#required' => TRUE,
);
return system_settings_form($form);
}
I modified it a little but it does work. What I want to do is do this but run my own functions and code on here and not the "drupal_get_form" function.
I tried to do this and just created a function to echo text and then put the function name in the "page callback" field of the array. This did work, it did execute my function on the page instead of the drupal form function, but the page was a blank white page with none of the "drupal backend styling or menus or anything"; it was literally just completely blank white webpage with just my text printed on it.
So I am thinking the "drupal_get_form" function not only puts a form on the page, but it also makes it so it is drupal backend page with proper header, footer, menus etc.
So I am thinking that i need a function like "drupal_get_form" but it has a "blank slate" where I can run whatever code or functions that I want.
Would anybody know anything about this or how to approach doing this?
Thanks so much...
Everything you need is to create a template, register it and use theme function in your code. You can take a look at this Quick Introduction to Drupal's hook_menu() and hook_theme()
So your code might look like:
function add_game_menu() {
$items = array();
$items['admin/add_game'] = array(
'title' => 'Add Gm Pg',
'description' => 'Description of your add game page',
'page callback' => 'my_function',
'access arguments' => array('administer add_game settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_function(){
// Call theme() function, so that Drupal includes the custom-page.tpl.php template
return theme('my_custom_template');
}
/*
* Implementation of hook_theme().
*/
function add_game_theme(){
return array(
'my_custom_template' => array(
// template file name will be custom-page.tpl.php
'template' => 'custom-page',
),
);
}

Create custom form display it in colorbox in each page in drupal 7?

I want to display custom form (which is custom module) in colorbox in each page.
I have created form in drupal 7 which is working fine if i run it by calling it in url but i need to call this module in colorbox.
my custom module(regform) code is
function regform_init() {
drupal_set_message('Our module is alive!');
}
function regform_menu() {
$items = array();
$items['regform'] = array(
'title' => 'Custom page',
'page callback' => 'drupal_get_form',
'page arguments' => array('regform_form'),
'access arguments' => array('access content'),
'access callback' => TRUE
);
return $items;
}
function regform_form($form, &$form_state) {
$form['#suffix'] = '<div id="test-ajax"></div>';
$form['email'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'Enter Email',
'#size' => 50,
'#maxlength' => 50,
'#required' => TRUE, //make this field required
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#validate' => 'regform_form_validate',
'#ajax' => array(
'callback' => 'regform_form_ajax_callback',
'wrapper' => 'test-ajax'
),
'#submit' => array('regform_form_ajax_callback'),
);
return $form;
}
function regform_form_ajax_callback($form, &$form_state) {
/*Fire database query*/
/*Validation msg div block call here*/
return "<div id='test-ajax'></div>";
}
function regform_form_validate($form, &$form_state) {
if (trim($form_state['values']['email']) == ''){
form_set_error('email', t('Please Enter Email'));
}
if(!valid_email_address($form_state['values']['email'])){
form_set_error('email', t('Enter Valid Email'));
}
}
function regform_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
$form_state['input'] = array();
}
This form is working fine.
Also I dont have any idea about how to use colorbox in drupal 7.
Any help will be appreciated.
Thanks in Advance.
I suggest to use chaos tools for modal popup form. Its easy to implement and integrate into Drupal 7.
A great example of Ctools modal form is given at here
For modal forms in Drupal, I highly suggest taking a look at chaos tools. It provides a great AJAX API (that can handle the form submission).
There is another module you can take a look at, modal forms. It's built on top of Ctools and provide an easy way to show forms in modal.

Drupal dynamic internal redirect

What I want is pretty simple. I have registered a path
function spotlight_menu() {
$items = array();
$items['congres'] = array(
'title' => 'Congres',
'title arguments' => array(2),
'page callback' => 'taxonomy_term_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
When this menu item is triggered, I want to redirect (without changing the url) to a taxonomy page, of which the term is chosen in a function that runs when this function is called.
How can I do this (especcially without changing the url) ?
You can't call taxonomy_term_page directly as your page callback as you'd need to provide a load function to load the term, which is just going to be too difficult with the setup you've got.
Instead, define your own page callback as an intermediary and just return the output from taxonomy_term_page directly:
function spotlight_menu() {
$items = array();
$items['congres'] = array(
'title' => 'Congres',
'page callback' => 'spotlight_taxonomy_term_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function spotlight_taxonomy_term_page() {
// Get your term ID in whatever way you need
$term_id = my_function_to_get_term_id();
// Load the term
$term = taxonomy_term_load($term_id);
// Make sure taxonomy_term_page() is available
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
// Return the page output normally provided at taxonomy/term/ID
return taxonomy_term_page($term);
}

Categories