简体   繁体   中英

drupal 7 custom block changed?

Is the hook_block has been changed? the following is drupal 6 example i have found on internet(http://eureka.ykyuen.info/2010/11/10/drupal-create-a-block/), there is no block shown in admin/structure/block,

     * Implementation of hook_block().

     */

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

            switch ($op) {

                    //Define the block

                    case 'list':

                            $blocks[0]['info'] = t('Block Info');

                            $blocks[0]['cache'] = BLOCK_NO_CACHE;

                            return $blocks;



                    case 'configure':

                            //TODO: block configurable parameters

                            $form = array();

                            return $form;



                    case 'save':

                            //TODO: save new configuration

                            return;



                    //Display the block

                    case 'view':

                            $block['subject'] = t('Block Subject');

                            $block['content'] = 'Block Content';

                            return $block;

            }

    }

it seems that, hook_block in drupal 7 has been changed, how to rewrite about code? anyone can provide hints/direction to me? thank you very much.

Your code works for Drupal 6.The implementation of hook_block is changed in Drupal 7. In Drupal 7 there are different hooks that should be used to serve your purpose.

Check more about hook_block here

In Drupal 7, your implementation of hook_block() would be changed to:

/**
 * Implements hook_block_info().
 */
function custom_block_info() {
  $blocks = array();

  $blocks['list'] = array(
    'info' => t('Block Info'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function custom_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'list':
      if (user_access('access content')) {
        $block['subject'] = t('Block Subject');
        $block['content'] = 'Block Content';
      }
      break;
  }

  return $block;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM