Right now, domain_block_view_switcher() returns a block renderable array, but the content is itself just pre-rendered #markup. One of our clients wants to be able to e.g. add domains to the switcher that are actually hosted outside of Drupal (some countries have opted into Drupal; others haven't!)

At any rate, it would be useful to be able to alter the list of domains (re-order them? disable some?) rather than rendering the markup too early. I think it's best practice to expose the contents of blocks to hooks like hook_block_view_alter() and hook_page_alter() if possible.

Will attach a patch for this in the first comment.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jp.stacey’s picture

Patch attached to return renderable array of element type '#theme' => 'links'.

jp.stacey’s picture

Example of how this patch is helping us:

/**
 * Implements hook_block_view_MODULE_DELTA_alter().
 *
 */
function ourtheme_block_view_domain_switcher_alter(&$data, $block) {
  // Client wants domain links to be to the top of the site - seem to
  // currently link to the current URL, deep in each other site
  foreach($data['content']['#links'] as &$link) {
    $link['href'] = preg_replace('!^(https?://[^/]+/?).*!', '$1', $link['href']);
  }

  // Client wants to add another domain
  $data['content']['#links'][] = array(
    'title' => 'Wales site',
    'href' => 'http://www.google.co.uk/',
    'absolute' => TRUE,
  );
}
agentrickard’s picture

Remember that this is really just for admin users. Domain Nav is provided for end users and should be more flexible.

But these are really great examples and a great report and patch!

Looking at the code, we might pass the entire $domain instead of just the id in the $links array.

agentrickard’s picture

We should look at Domain Alias as well, which provides its own block.

jp.stacey’s picture

Status: Needs review » Needs work
FileSize
3.21 KB

Sorry for the delay, @agentrickard: other projects got in the way. We're not using the navigation directly any more - the client has asked for enough customizations that we've gone for a rich-text box :( - so this work is of limited use to us, but I've re-rolled the patch based on what you mention above. To respond to your comments specifically:

  • Just for admin users. Woah, really? That wasn't clear to us. Maybe if that's the case then it needs to have a different name in future releases. No harm done, though!
  • Pass the entire $domain. You're quite right: once it's been loaded from the db, that makes sense. I'm now passing the whole $domain object in.
  • Domain Alias. We're not using Domain Alias on our current builds at all, so it's a bit tricky for us to get any examples for it. So I've left it as it is for now.

Incidentally, seeing as you mentioned its navigational elements, I've also fixed domain_nav in this new patch, to return a theme array instead, and that's rolled into this patch too. It would've been really easy to do directly, but: the domain_nav_render() function is assumed an exposed call, so I couldn't alter that directly without possibly breaking people's themes.

So to protect legacy code, I did as follows:

  • Renamed the gist of domain_nav_render() as a private function _domain_nav_theme_array()
  • Made domain_nav_block_view() call this new private function
  • Stubbed out a legacy domain_nav_render(), which also calls this new private function, then runs theme() on the resulting array and returns the markup.

So please find attached a patch that gets both domain_block_view_switcher() and domain_nav_block_view() returning theme arrays.

agentrickard’s picture

Status: Needs work » Needs review
agentrickard’s picture

Issue summary: View changes

Forgot to complete a sentence!