0

I am working on trying to understand the secrets of custom-developed blocks. Namely, I have a use case where a block need to pull content via EntityFieldQuery (this could be done with Views, but I like a challenge) and the contents loaded need to adjust based on a URL argument.

Right now my running assumption is that since the page that will deliver these results is run through Context, my best bet for getting values from the URL are through arg() when the block is generated inside its referencing callback from hook_block_view(). I just want to be sure this is the best approach to actually load values that must be passed in through this approach, and i'm not missing a more reasonable design pattern.

Code example is below.

/**
 * Implements hook_menu
 */
function widgets_and_springs_menu() {
  $items['springs/%'] = array(
    'title' => t('All Springs for Category'),
    'page callback' => 'widgets_and_stone_blank_page', //Returns theme of '', enough for Context to catch
    'type' => MENU_LOCAL_TASK,
    'page arguments' => array(1),
  );

  return $items;
}

/**
 * Implements hook_block_info()
 */
function widgets_and_springs_block_info() {
  $block['springs_listing'] = array(
    'info' => t('All Springs Per Category'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );

  return $block;
}

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

  switch($block) {
    case "springs_listing":
      $block['subject'] = "";
      $block['content'] = widgets_and_springs_get_results_by_uri_arg();
  }
}

/**
 * Returns arguments by URL. First item MUST contain a TID argument.
 */
widgets_and_springs_get_results_by_uri_arg() {
  $searchTid = arg(1);
  //Other stuff & Return of results within theme()...
}

1 Answer 1

0

It's not uncommon to customize block contents based on URL arguments. I don't see any reason to not do that.

However, since your block contents can change based on the URL, you need to use either no cache at all or DRUPAL_CACHE_PER_PAGE.

If your block contains any Ajax elements, note that arg() will return the ajax path.

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.