Rectangle 27 5

For others who may hit this page, if you're just working with standard callbacks (not necessarily views), this is easy. In your callback function, instead of returning the code to render within the page, use the 'print' function.

function mymodule_do_ajax($node)
{
    $rval = <<<RVAL
        <table>
            <th>
                <td>Data</td>
                <td>Data</td>
                <td>Data</td>
            </th>
            <tr>
                <td>Cool</td>
                <td>Cool</td>
                <td>Cool</td>
            </tr>
        </table>
RVAL;

    //return $rval; Nope!  Will render via the templating engine.
    print $rval; //Much better.  No wrapper.
}

php - displaying a Drupal view without a page template around it - Sta...

php drupal drupal-views
Rectangle 27 3

Another way to do it which I find very handy is to add a menu item with a page callback function that doesn't return a string:

/**
 * Implementation of hook_menu.
 */
function test_menu(){
  $items['test'] = array (
    /* [...] */ 
    'page callback' => 'test_callback',
    /* [...] */ 
  );
  return $items;
}

function test_callback() {
  // echo or print whatever you want
  // embed views if you want
  // DO NOT RETURN A STRING
  return TRUE;
}

It would be much better to use exit(); instead of return TRUE; (see comment).

This will cause a bunch of warnings if you're using ctools (ctools_page_alter will assume that it's an array).

php - displaying a Drupal view without a page template around it - Sta...

php drupal drupal-views
Rectangle 27 2

In addition to berdir's comment, you can also skip the intermediate callback function and just call views_embed_view directly from your menu router:

function hook_menu(){
   $items['path/to/my/view'] = array(
    'title' => 'Report', 
    'page callback' => 'views_embed_view',
    'page arguments' => array('my-view-name'),
    'access arguments' => array('access content'), 
    'type' => MENU_NORMAL_ITEM,
  );    
  return $items;
}

php - Drupal 6: Make a menu_hook() return a specific view - Stack Over...

php drupal drupal-6 drupal-views drupal-modules
Rectangle 27 2

In your page callback function, if you just print the content of your page without returning anything, it won't be included within your page.tpl.php. If you return a string containing the contents of your page, it will go into the content region, i.e.

function mymodule_page_callback () {
   $content = "Hello, world inside the content region";
   return $content;
}

You want to call me a moron publicly or via a private message? Of course it was that easy. - thanks.

@Ward, you'll hear no name calling from me. It's not that well documented. Believe me, I know. I recently pulled my hair out trying to do the opposite (for ajax purposes).

YES! Agree with Karl that there is some hair pulling when it comes to ajax, and it's not documented clearly.

Drupal - Template for Custom Page - Stack Overflow

drupal templates
Rectangle 27 3

#Here is the simple code for creating form in module#

===============================================================

/*..firstly create a menu in module by copying this code..*/
function form_test_menu() {
    $items['formtest'] = array(
                        'title' => 'Form Test',
                        'page callback' => 'drupal_get_form',
                        'page arguments' => array('form_test_form'),
                        'access callback' => TRUE,
                        );
    return $items;
}

/*...Now create fields like below...*/
function form_test_form($form,&$form_submit) {
    $form['firstname'] = array(
                            '#title' => t('Firstname'),
                            '#type' => 'textfield',
                            '#required' => TRUE,
                            );
    $form['lastname'] = array(
                            '#title' => t('Lastname'),
                            '#type' => 'textfield',
                            );
    $form['submit'] = array(
                        '#value' => 'Submit',
                        '#type' => 'submit',
                        );
    return $form;
}

php - How to create a form inside a drupal module? - Stack Overflow

php drupal drupal-7 drupal-modules drupal-fapi
Rectangle 27 2

As mentioned by Clive, You can achieve this by ajax call. You need to setup page callback under menu hook in your custom module & there you can mention the function to call on page request.

Example:
// this code will go under menu hook
$items['ajax/rule1.1'] = array(
    'title' => 'Rule 1.1', 
    'page callback' => 'custom_rule_trigger', 
    'access arguments' => array('access content'), 
    'file' => 'custom_rules.inc',
  );

// then you can write php code which you want to execute under custom_rule_trigger function in custom_rules.inc file.
function custom_rule_trigger(){
// write your rules here.. 
}
www.example.com/ajax/rule1.1

How to trigger a drupal rule from a javascript code - Stack Overflow

javascript drupal drupal-7 drupal-rules
Rectangle 27 1

/**
 * Implements hook_page_alter().
 *
 * Adds a post_render callback
 */
function MYMODULE_page_alter(&$page) {
  $page['#post_render'][] = 'MYMODULE_CALLBACK';
}

/**
 * Implements callback_post_render().
 *
 * Inserts JavaScript snippet immediately following the opening body tag.
 */
function MYMODULE_CALLBACK(&$children, $elements) {
  $script = '<script type="text/javascript">console.log(\'hello world\');</script>';
  // Insert snippet after the opening body tag.
  $children = preg_replace('@<body[^>]*>@', '$0' . $script, $children, 1);
  return $children;
}

drupal - Insert code right after body tag using theme functions - Stac...

drupal themes hook override
Rectangle 27 1

'access callback' should be a function returning a boolean value, and 'access callback' => TRUE will work fine. Anyway i would suggest you not to use a constant value, for a cleaner access control. Use

'access callback' => 'user_access',
'access arguments' => array('access content'),

instead, that's one generic enough permission. Or try the permission better fitting your needs. You can omit the access callback itself, since user_access is used as the default one.

php - Drupal - how to register a MENU_CALLBACK that is accessable to a...

php drupal drupal-6
Rectangle 27 1

Add the line after + sign and clear cache after that check it again.
function registration_menu() {
  $items['registration/%registration'] = array(
    'title callback' => 'registration_page_title',
    'title arguments' => array(1),
    'page callback' => 'registration_view',
    'page arguments' => array(1),
    'access callback' => 'entity_access',
    'access arguments' => array('view', 'registration', 1),
+   'type' => MENU_NORMAL_ITEM,
  );

Drupal: How to display a menu item of access denied and redirect it to...

drupal drupal-7 drupal-modules
Rectangle 27 1

function hook_menu_alter(&$items) {
   $items['node/%node/edit']['access callback'] = my_permission_function;
}

function my_permission_function($node){
  if( (node_type is 1 and tax_of_node is tax2)|| (node_type is 2 and tax_of_node is tax1)
     return TRUE;
  else return FALSE;
}

Not the exact code. Follow the logic. Try something like this and it should work.

Thanks for this advice. I think I'd prefer a way to do it at the grant level, but it could be a useful workaround.

drupal - Give edit permissions to roles based on taxonomy and content ...

drupal permissions content-type taxonomy
Rectangle 27 1

Add another callback to the array of functions that are called when the node's edit form is submitted. You'll need to understand the basics of the form API. It's not too hard.

function my_function() {
  //do stuff to the other node
}
function my_function()
$form['#submit'][] = 'my_function';

Yes, that's right. You'll need to make yourself a module. Your $form modification goes inside a hook_form_later (which goes in your module). It alters the form to add your extra functionality. That's the beauty of Drupal. It lets you extend existing functionality easily :)

Drupal 6, Create a form in a node which save data to another content t...

drupal-6 cck drupal-fapi
Rectangle 27 0

It is not especially 'easy', but the standard way to do it would be to use Drupals Forms API for the callback submission as well - that way, you'll get the same validation that would happen on a non js submit.

Take a look at Adding dynamic form elements using AHAH. While it does not match your scenario exactly (they rebuild the form on the callback to add new elements, not to save data), the explanation of the processing workflow is pretty helpful.

Then there are several modules that try to offer AJAX form submission in a generic way - you could check their code on how to do it (or maybe just use them ;)

php - Drupal 6 Validation for Form Callback Function - Stack Overflow

php validation drupal forms drupal-fapi
Rectangle 27 0

function example_uc_price_handler() {
  return array(
    'alter' => array(
      'title' => t('Reseller price handler'),
      'description' => t('Handles price markups by customer roles.'),
      'callback' => 'example_price_alterer',
    ),
  );
}

function example_price_alterer(&$price_info, $context, $options = array()){
  global $user;
  if (in_array("reseller", $user->roles)) { //Apply 30% reseller discount
    $price_info["price"] = $context["subject"]["node"]->sell_price - (
                           $context["subject"]["node"]->sell_price * 0.30) ;     
  }
  return;
}

Drupal/Ubercart custom price php code for roles - Stack Overflow

php drupal roles ubercart
Rectangle 27 0

Instead of hacking, you could make sure that the path you are querying "/_/city?n=" is a valid menu_hook item. That way it will validate against the drupal_valid_path() inside theme_textfield(). From within the menu hook function callback you could then forward the request to your external data source.

Yes and no :) Rerouting the request thru a real menu-hook would jeopardize my performance-efforts which were the initial reason to use an external script. I tried to create a similar named menu-hook to allow drupal to validate the url. The RewriteRule in .htaccess prevents accessing Drupal-urls if a similar file does exist in the file-system so it would have validated against the drupal callback and would have used the external script. The problem was the special url which contains ?n=. I was not able to create a matching callback.

drupal - Autocomplete without using a callback - Stack Overflow

drupal drupal-6 autocomplete
Rectangle 27 0

First of all you won't be able to hide them dynamically by making them MENU_CALLBACK. What you should do is change the access callback to a function where you would check for the current user.

As for why you can't find the tabs, check if:

  • There isn't a menu entry with %views_arg instead of %user
  • That your module's weight (in the system table) is bigger than views' weight. Since views uses the same hook it's possible that your module gets called before views.

The tab does indeed use an argument from views, but replacing %user with %views_arg does not seem to work.

Yup, correct! I was sure the module weight was high, but it wasn't... working now...

Drupal - Modify tabs added to user page by Views - Stack Overflow

drupal drupal-6 drupal-views
Rectangle 27 0

As you are referring to code to put in a module, then the module should implement hook_theme_registry_alter(), which would allow the module to override the function theme_menu_local_tasks(). The module should store the value of the previous callback, so that it could still call it in the case the page it not one that it should change. Implementing a hook in the module allows you to have the normal menu tabs, once the module has been disabled; altering the current theme would require you to change it back when you want the functionality anymore, and if you are using a theme made from another person you should change the theme all time you download a new version. If you are using more than one theme, then you should make the change to each used theme. In general, a modification to a theme that is required from a module should be done inside a module.

How to alter a MENU_LOCAL_TASK tab menu in Drupal - Stack Overflow

drupal drupal-6
Rectangle 27 0

Wow I can't believe I didn't see that. OK so now I am having a different issue. Actually two. So I fixed the code in the menu link, but for some reason the value that I pass in the URL is getting cut off. In other words if I go to /job-seekers/update-resume/2058569, my hidden form field only contains "2". Also my form submit button does nothing. The form renders and I can choose a file to upload, but clicking the submit button does not submit the form. I updated my original post to show you what I am using as my callback function to render the form.

Passing URL parameters to a Drupal form created in a menu item - Stack...

forms drupal drupal-7
Rectangle 27 0

Just in case anyone is still having trouble with this I found a great solution while trying to figure out how to do this. I had a year select list and that dictated what data was displayed in the autocomplete field. The solution basically has an ajax callback function for the select list that can then update the autocomplete field with an extra parameter in the url. Anyways, it is really well explained in the following article.

*A note of caution, I was going crazy trying to figure out why it did not work and it turns out you can't have the same form on the page twice (I needed to because I was displaying it differently for mobile devices) because you are using an id for the ajax callback. I added an extra argument to accomplish that. It is called uniqueid in the below example.

function report_cards_comparison_form($form, &$form_state, $uniqueid) {

   $curryear = t('2012');

   $form['year_select'] = array(
        '#title' => t('School Year'),
        '#type' => 'select',
        '#options' => array(
            '2012' => t('2012'),
            '2013' => t('2013'),
            '2014' => t('2014'),
            '2015' => t('2015'),
          ),
        '#default_value' => $curryear,
        '#ajax' => array(
          'callback' => 'report_cards_comparison_form_callback',
          'wrapper' => $uniqueid,
          'progress' => array(
                'message' => 'Updating Schools...',
                'type' => 'throbber'
            ),
        ),

  ); 

  $form['choice'] = array(
    //'#title' => t('Search By:  School Name'),
    '#type' => 'textfield',
    '#attributes' => array(
        'class' => array('school-choice'),
        'placeholder' => t('Start Typing School Name...'),
    ),
    '#required' => TRUE,
    '#autocomplete_path' => 'reportcards/autocomplete/' . $curryear,
    '#prefix' => '<div id="' . $uniqueid . '">',
    '#suffix' => '</div>',
  ); 

  $form['submit'] = array(
    '#type' => 'submit',
    '#prefix' => '<div class="submit-btn-wrap">',
    '#suffix' => '</div>',
    '#value' => t('Search'),
    '#attributes' => array('id' => 'add-school-submit'),
  );

  return $form;
}

/**
 * Ajax Callback that updates the autocomplete ajax when there is a change in the Year Select List
 */
function report_cards_comparison_form_callback($form, &$form_state) {
  unset($form_state['input']['choice'], $form_state['values']['choice']);
  $curryear = $form_state['values']['year_select'];

  $form_state['input']['choice'] = '';
  $form['choice']['#value'] = '';
  $form['choice']['#autocomplete_path'] = 'reportcards/autocomplete/' . $curryear;

  return form_builder($form['#id'], $form['choice'], $form_state);
}

and I can call the form by doing this...

print render(drupal_get_form('report_cards_comparison_form', 'desktop-schoolmatches'));

Drupal autocomplete, callback with multiple parameters - Stack Overflo...

drupal autocomplete callback drupal-7
Rectangle 27 0

I had the same problem and have figured out a way, which is not too strenuous. It involves overriding the textfield theme and then passing your parameter to the theme function.

function mymodule_theme() {
  $theme_hooks = array(
    'my_module_autocomplete' => array(
      'render element' => 'element',
    ),
  );
  return $theme_hooks;
}

Next we need to add the theme and the variable to our form element. In my case, the form element is part of a field widget:

function my_module_field_widget_form($form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  if($instance['widget']['type'] == 'my_module_field_type') {

    $element['my_module_field'] = array(
      '#type' => 'textfield',
      '#autocomplete_path' => 'my-module/autocomplete',
      // THIS IS THE IMPORTANT PART - ADD THE THEME AND THE VARIABLE.
      '#theme' => 'my_module_autocomplete',
      '#my_module_variable' => $field['field_name'],
    );
  }
  return $element;
}

Then implement the theme function. This is a copy of theme_textfield from includes/form.inc with one important difference - we append the variable to the autocomplete path:

function theme_my_module_autocomplet($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'text';
  element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
  _form_set_class($element, array('form-text'));

  $extra = '';
  if ($element['#autocomplete_path'] &&     drupal_valid_path($element['#autocomplete_path'])) {
    drupal_add_library('system', 'drupal.autocomplete');
    $element['#attributes']['class'][] = 'form-autocomplete';

    $attributes = array();
    $attributes['type'] = 'hidden';
    $attributes['id'] = $element['#attributes']['id'] . '-autocomplete';
    // THIS IS THE IMPORTANT PART. APPEND YOUR VARIABLE TO THE AUTOCOMPLETE PATH.
    $attributes['value'] = url($element['#autocomplete_path'] . '/' . $element['#my_module_variable'], array('absolute' => TRUE));
    $attributes['disabled'] = 'disabled';
    $attributes['class'][] = 'autocomplete';
    $extra = '<input' . drupal_attributes($attributes) . ' />';
  }

  $output = '<input' . drupal_attributes($element['#attributes']) . ' />';

  return $output . $extra;
}

Now the variable will be available as the first parameter on the autocomplete callback function:

function _my_module_autocomplete($my_module_variable, $search_string) {
  // Happy days, we now have access to our parameter.
}

Drupal autocomplete, callback with multiple parameters - Stack Overflo...

drupal autocomplete callback drupal-7
Rectangle 27 0

function myfunction() {
  drupal_set_header('Content-Type: text/plain');
  print "some text";
  exit(0);
}

+1 - This is the way Drupal itself returns 'unthemed' content, e.g. answers to js callbacks or files (when private files are enabled). See the api.drupal.org/api/function/file_transfer/6 for an example of this in core.

A word of warning applies: When ending the normal Drupal request processing via exit()/die(), hook_exit will not be invoked, so make sure not to rely on that in these cases.

Instead of exit; generally drupal_exit(); should be used in Drupal 7: api.drupal.org/api/drupal/includes%21common.inc/function/ This invokes hook_exit() and more.

This question is about Drupal 6 but, this answer would apply to Drupal 7 too if we use drupal_add_header api.drupal.org/api/drupal/includes!bootstrap.inc/function/ instead of drupal_set_header

returning untemplated output in drupal from menu callback function - S...

drupal drupal-6