Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am using Drupal 6.27, created my own module, used hook_perm() in my module. Now the question is - did i do that in the right way?

/**
 * Implementation of hook_block().
 */

function adminka_block($op = 'list', $delta = 0, $edit = array()) {
  switch($op) {
    case 'list':
      return array(
        'adminka_simple_block' => array('info' => 'adminka simple block', 'cache' => BLOCK_NO_CACHE),
      );
    case 'view':
      switch($delta) {
        case 0:
          if (user_access('view adminka')) {
            $block['subject'] = t('My admin panel');
            $block['content'] = '<a href="/node/add/' . variable_get('content_type', false) . '">Добавить ' . variable_get('content_type_name', false) . '</a><br />';
            $block['content'] .= '<a href="/logout">Выход</a>';
            break;
          }
      }
      return $block;
  }
}

Because i didn't found another way to restrict that block visiblity only to users with user_acess('view adminka').

share|improve this question
add comment

2 Answers

I think you do it in the right way, but instead of using this

$block['content'] = '<a href="/node/add/' . variable_get('content_type', false) . '">Добавить ' . variable_get('content_type_name', false) . '</a><br />';
$block['content'] .= '<a href="/logout">Выход</a>';

you should use l function

This function correctly handles aliased paths, and adds an 'active' class attribute to links that point to the current page (for theming), so all internal links output by modules should be generated by this function if possible.

like this:

$block['content'] .= l(t('Выход'), '/logout');
share|improve this answer
add comment

Regarding perms, yes, you are doing that part right. However, I think the $delta case value in your view should be adminka_simple_block not 0 and this is why I like to set up blocks like this instead because of the ease of adding more as your module gets bigger....

function adminka_block($op = 'list', $delta = 0, $edit = array()) {

  $blocks=array();

  switch($op) {

    case 'list':

      $blocks['admin']['info'] = 'adminka admin block';
      $blocks['admin']['cache'] = BLOCK_NO_CACHE;

      $blocks['another']['info'] = 'adminka another block';

      return $blocks;

    case 'view':

      switch($delta) {

        case 'admin':

          if (user_access('view adminka')) {

            $blocks['subject']  = t('My admin panel');
            $blocks['content']  = l('Добавить ' . variable_get('adminka_content_type_name', NULL), 'node/add/' . variable_get('adminka_content_type', NULL));
            $blocks['content'] .= l('Выход', 'logout');

          }

          return $blocks;

        case 'another':

          $blocks['subject']  = t('Another Block');
          $blocks['content']  = 'Here we have another block';
          $blocks['content'] .= ' with more content, etc...';
          $blocks['content'] .= ' with default caching.';

          return $blocks;

      }
   }
}

I will admit I am not up on translation stuff, but I think you may want to think about using t() to do more here, but that's a whole nother ball of wax :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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