0

Hi I am trying to create a custom block but it is not appearing in the block listing is there something missing in my code.

   <?php

  $view = views_get_view('schedule_list2');
  $view->execute();
  print $view->render();

  $view = views_get_view('next');
  $view->execute();
  print $view->render();
/**
 * Implements hook_block_info().
 */
function custom_block_block_info() {
  $blocks = array();
  $blocks['my_block'] = array(
    'info' => t('My Custom Block'),
  );

  return $blocks;
}
/**
 * Implements hook_block_configure().
 */
function custom_block_block_configure($delta='') {
  $form = array();

  switch($delta) {
    case 'my_block' :
      // Text field form element
      $form['text_body'] = array(
        '#type' => 'text_format',
        '#title' => t('Enter your text here in WYSIWYG format'),
        '#default_value' => variable_get('text_variable', ''),
      );

      // File selection form element
      $form['file'] = array(
        '#name' => 'block_image',
        '#type' => 'managed_file',
        '#title' => t('Choose an Image File'),
        '#description' => t('Select an Image for the custom block.  Only *.gif, *.png, *.jpg, and *.jpeg images allowed.'),
        '#default_value' => variable_get('block_image_fid', ''),
        '#upload_location' => 'public://block_image/',
        '#upload_validators' => array(
          'file_validate_extensions' => array('gif png jpg jpeg'),
        ),
      );
      break;
  }
  return $form;
}

/**
 * Implements hook_block_save().
 */
function custom_block_block_save($delta = '', $edit = array()) {
  switch($delta) {
    case 'my_block' :
      // Saving the WYSIWYG text      
      variable_set('text_variable', $edit['text_body']['value']);

      // Saving the file, setting it to a permanent state, setting a FID variable
      $file = file_load($edit['file']);
      $file->status = FILE_STATUS_PERMANENT;
      file_save($file);
      $block = block_load('custom_block', $delta);
      file_usage_add($file, 'custom_block', 'block', $block->bid);
      variable_set('block_image_fid', $file->fid);
      break;
  }
}
?>
<?php
/**
 * Implements hook_block_view().
 */
function custom_block_block_view($delta='') {
  $block = array();
  switch($delta) {
  case 'my_block' :
    $block['content'] = my_three_upcoming_events();
    break;
  }
  return $block;
}
/**
 * Custom function to assemble renderable array for block content.
 * Returns a renderable array with the block content.
 * @return
 *   returns a renderable array of block content.
 */
function my_three_upcoming_events() {
  $output = '';
  $now_events = views_get_view_result('schedule_list2');
  $upcoming_events = views_get_view_result('next');
  $max_events = 3;
  // Make a big list of events now; upcoming events will be on the end of this list.
  $all_events = $now_events + $upcoming_events;

  for ($i=0,$z=$max_events;$i<$z;$i++) {
    $node = node_load($all_events[$i]['nid']);
    $output .= '<div>' . l($node->title, '/node/'.$node->nid) . '</div>';
  }
  return $output;
}
?>
3
  • Where are you putting this code? I ask because the views related code at the top looks odd if it is a custom module.
    – rooby
    Mar 26, 2014 at 11:09
  • Just to cover all bases, is your module called custom_block and is enabled? and you have tried clearing the drupal cache?
    – rooby
    Mar 26, 2014 at 11:11
  • Hi Rooby I have called it custom_block removed the code at the top and its now working. So put your last comment as a answer and will select as a correct one Mar 26, 2014 at 11:44

1 Answer 1

0

Make sure your module is named custom_block to match the function names you have used for your hooks. If you have to change the name of your module you will need to re-enable it. Technically you should uninstall the module, then rename and re-enable - unless it isn't properly installed ans you can't uninstall it. This will keep unwanted data out of your database.

You also should not be printing things outside of functions in module files, like the views code at the top is doing.

Printing markup should be done at the theme level in templates, or in appropriate functions in your module (like you are going in your hook_block_view()).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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