1

The purpose of function civicrm_metatag_metatags_view_alter() is to ensure the metatag URLs include the query parameters. The problem is that it acts on non-CiviCRM pages.

'Metatag' is a Drupal module for adding tags to the rendered HTML, and this function is in drupal/civicrm.module.

Proposed solution

civicrm_metatag_metatags_view_alter() should be modified to only work on CiviCRM pages. With all kudos going to @Chris-Burgess I've created an issue CRM-17594 and PR https://github.com/civicrm/civicrm-drupal/pull/328 to resolve this.

My question: How do I check that a URL is a CiviCRM URL?

As discussed below, the answer is to check if arg(0) equals 'civicrm'.

Description of problem

I get the following PHP Notice ...

Notice: Array to string conversion in _civicrm_get_url_parameters_as_url_string() (line 295 of .../sites/all/modules/civicrm/drupal/civicrm.module).

A page where this occurs is http://citybibleforum.org/library?&title=&field_speakers_tid=&field_topics_tid=&field_bible_reference_value=&field_episode_date_value%5Bmin%5D=2011-08-12&field_episode_date_value%5Bmax%5D=2012-08-12&field_is_audio_value=All&field_is_text_value=All&field_is_video_value=All&page=46

The parameters causing the notice are ...

field_episode_date_value[min]=2011-08-12&field_episode_date_value[max]=2012-08-12

0

I'd recommend a check to test that the Drupal path (eg current_path()) commences with civicrm. This is untested, but I think you want something like the following -

/**
 * Alter metatags before being cached.
 *
 * This hook is invoked prior to the meta tags for a given page are cached.
 *
 * @param array $output
 *   Metatags to be displayed.
 * @param string $instance
 *  Context.
 */
function civicrm_metatag_metatags_view_alter(&$output, $instance) {
  if (arg(0) == 'civicrm') {
    $linkUrls = array('og:url', 'canonical', 'shortlink');
    foreach ($linkUrls as $url) {
      if (isset($output[$url]['#attached']['drupal_add_html_head'][0][0]['#value'])) {
        $output[$url]['#attached']['drupal_add_html_head'][0][0]['#value'] .= _civicrm_get_url_parameters_as_url_string();
      }
    }  
  }
}

I don't really like the look of the fixed [0][0] in that code either?

If you're submitting a fix, you should check out CRM-16495: Metatag canonicals are broken, q is multiplied which looks like it fixes the error message you're experiencing also. That fix is committed to 4.7 now.

I think your proposed solution is correct, CiviCRM shouldn't be messing with metatags if it hasn't been involved in the page generation.

  • Thanks Chris! That's what I expected, but I vaguely remembered coming across a case where it's possible to change the relative path civicrm uses. Is that possible, or am I mis-remembering? – Ken West Nov 18 '15 at 23:01
  • Relocating CiviCRM can probably be done eg with rewrites at the webserver level, but IMO Drupal would have civicrm at the start of both request_path() and current_path(). Doing it another way (hook_menu_alter()?) would probably cause bigger issues than just with metatags - I'd be interested to know how else it can be done, but that's definitely another question :D – Chris Burgess Nov 18 '15 at 23:04
  • Thanks again, Chris! I'll apply the patch to CRM-16495. Another question: in civicrm.module there are other tests checking if this is a CiviCRM page (I should have found these earlier!). For instance, the function civicrm_on_user_page() tests isset($_POST['_qf_default']) and other functions use tests of the form arg(0) == 'civicrm'. Rather than introduce a new test using stristr(), would we be able to reuse one of those tests? – Ken West Nov 18 '15 at 23:19
  • If I've answered your question right, please mark the answer accordingly? Note that the patch to CRM-16495 doesn't solve your question, I think it just addresses the warning you mentioned. – Chris Burgess Nov 18 '15 at 23:22
  • arg(0) == 'civicrm' is a better way of detecting whether we're in CiviCRM than the one I proposed, yes. isset($_POST['_qf_default']) tests whether an HTML_QuickForm post is being made. – Chris Burgess Nov 18 '15 at 23:23

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.