0

I'm kind of new to drupal (been working with it for 2 months) and i seem to hit a wall with this issue . I've written a little module to allow my users to find phone numbers quickly . It uses autocomplete and works perfectly fine when the user is logged in , but fail when it ain't the case (no icon in the field no http requests fired).

Digging a little further , i can see that neither the autocomplete.js is loading nor the targeted field is properly formatted for autocomplete .

<form action="/" method="post" id="annuchum-form" accept-charset="UTF-8"><div><div class="form-item form-type-textfield form-item-search">


<label for="edit-search">Nom de l'agent </label>
 <input type="text" id="edit-search" name="search" value="" size="30" maxlength="128" class="form-text" />
<div class="description">Tapez une recherche et cliquez votre choix pour obtenir le numéro</div>
</div>

Anybody got any idea ? Maybe I missed something as I'm a seasoned php Dev but a total Drupal Newbie .I've searched the whole net looking for a clue

here my .module

<?php

/**
 * @file
 * Un module pour la récupération de données d'annuaire , et leur présentation sous UI Web 2.0
 */

/**
 * Immplements hook_block_help().
 * @param $path
 * @param $arg
 * @return string
 */
function annuchum_help($path,$arg)
{
    switch($path)
    {
        case "admin/help#annuchum":
            return '<p>' . t("Annuaire du Centre Hospitalier Universitaire") . '</p>';
        break;
    }
}

/**
 * Implements hook_block_info()
 */

function annuchum_block_info()
{
    $blocks['annuchum'] = array(
        'info' => t('Annuaire CHUM'),
        'cache'=> DRUPAL_CACHE_PER_ROLE,
    );

    return $blocks;
}

function annuchum_block_view($delta='')
{
    switch($delta)
    {
        case 'annuchum':
            $block['subject'] = t('Annuaire CHU ');
            $block['content'] = drupal_get_form('annuchum_form');
            break;
    }

    return $block;
}

function annuchum_form()
{
    $form =array();

    $form['search'] = array(
        '#title' => t('Nom de l\'agent'),
        '#type' => 'textfield',
        '#size' => 30,
        '#autocomplete_path' => 'annuchum/autocomplete',
        '#description' => 'Tapez une recherche et cliquez votre choix pour obtenir le numéro',
    );

    $form['annulink'] =array('#markup' => "<a href='http://portail.fr:81/?q=annuaire-interne'>Annuaire détaillé</a>");

    return $form;
}

function annuchum_menu()
{
    $items['annuchum/autocomplete'] = array(
        'page callback' => '_annuchum_autocomplete',
        'title' => 'autocomplete pour annuaire',
        //'access callback' => TRUE,
        //'access' => user_access('access annuchum autocomplete'),
        'access arguments' => TRUE,
        'type' => MENU_CALLBACK,
        'delivery callback' => 'drupal_json_output',
    );
    return $items;
}

function _annuchum_autocomplete($string ='')
{
    $matches = array();

    $res = db_query("SELECT node.nid , node.title,field_prenom_value,field_tel_value
          FROM field_data_field_tel, field_data_field_prenom, node
          WHERE field_data_field_tel.entity_id = node.nid
          AND field_data_field_prenom.entity_id = node.nid
          AND node.type = 'annuaire'
          AND node.title LIKE LOWER(:ustring) ",array(':ustring' => '%' . db_like($string) . '%'));

    foreach($res as $node)
        if(strlen($node->title) + strlen($node->field_prenom_value) < 15)
            $matches[$node->field_tel_value] = check_plain($node->title . " " .$node->field_prenom_value . " " . str_replace(" ","",$node->field_tel_value));
        else
            $matches[$node->field_tel_value] = check_plain($node->title . " " .substr($node->field_prenom_value, 0 , 12 - strlen($node->title) > 0 ? 12 - strlen($node->title): 1) . ". " . str_replace(" ","",$node->field_tel_value));

    //print_r($matches);
    //echo 'alive';
    //return $matches;
    drupal_json_output($matches);
}
?>

Sorry for poor grammar , i'm not a native speaker . Thanks in advance :)

2 Answers 2

2

You're so close...

access arguments should be an array of arguments to pass to the function in access callback. If access callback isn't explicitly defined, user_access() is used by default.

user_access() can take a single argument (the permission string you want to check access for, against the current user), so you need to pass a permission string as the first argument of access arguments.

I haven't checked, but I imagine Drupal's clever enough not to add the autocomplete widget in full if access to the path is denied, which it currently will be as you're not providing enough access information for the system to make a decision (the default is pessimistic, to deny access).

Using your existing code as an example I'm pretty sure this is what you're looking for:

$items['annuchum/autocomplete'] = array(
    'page callback' => '_annuchum_autocomplete',
    'title' => 'autocomplete pour annuaire',
    'access arguments' => array('access annuchum autocomplete'),
    'type' => MENU_CALLBACK,
    'delivery callback' => 'drupal_json_output',
);

After you clear caches the changes should take effect, assuming that you've granted access annuchum autocomplete permissions for the anonymous role.

For a much more in-depth look at hook_menu(), this question/answer is basically required reading

1
  • Ok , got it working , it was really more of a didn't RTFM issue so i'm ashamed , but i was already searching all over the (wrong) places so you saved my day. Thanks a lot for putting me back on track :)
    – DaKi
    Jul 11, 2013 at 13:23
0

In hook_menu() you should comment out 'access arguments' and uncomment 'access callback'.

$items['annuchum/autocomplete'] = array(
    'page callback' => '_annuchum_autocomplete',
    'title' => 'autocomplete pour annuaire',
    'access callback' => TRUE,
    //'access' => user_access('access annuchum autocomplete'),
    //'access arguments' => TRUE,
    'type' => MENU_CALLBACK,
    'delivery callback' => 'drupal_json_output',
);
return $items;
1
  • Naked hook_menu() items are almost always a bad idea.
    – mpdonadio
    Jul 10, 2013 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.