Answers.Body: <p>I'd recommend a check to test that the Drupal path (eg <a href="https://api.drupal.org/api/drupal/includes%21path.inc/function/current_path/7" rel="nofollow"><code>current_path()</code></a>) commences with <code>civicrm</code>. This is untested, but I think you want something like the following -</p>
  
  <pre><code>/**
   * 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(&amp;$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();
        }
      }  
    }
  }
  </code></pre>
  
  <ul>
  <li><a href="http://cgit.drupalcode.org/metatag/tree/metatag.api.php#n215" rel="nofollow"><code>hook_metatag_metatags_view_alter()</code></a></li>
  <li><a href="https://github.com/civicrm/civicrm-drupal/blob/7.x-master/civicrm.module#L263" rel="nofollow"><code>civicrm_metatag_metatags_view_alter()</code></a></li>
  </ul>
  
  <p>I don't really like the look of the fixed <code>[0][0]</code> in that code either?</p>
  
  <p>If you're submitting a fix, you should check out <a href="https://issues.civicrm.org/jira/browse/CRM-16495" rel="nofollow">CRM-16495: Metatag canonicals are broken, q is multiplied</a> which looks like it fixes the error message you're experiencing also. That fix is committed to 4.7 now.</p>
  
  <p>I think your proposed solution is correct, CiviCRM shouldn't be messing with metatags if it hasn't been involved in the page generation.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-18T23:01:00.623Z
Answers.Text: 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?
Answers.UserDisplayName: Ken West
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-18T23:04:25.470Z
Answers.Text: 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()`](https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_path/7) and [`current_path()`](https://api.drupal.org/api/drupal/includes%21path.inc/function/current_path/7). 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
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-18T23:19:39.110Z
Answers.Text: 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?
Answers.UserDisplayName: Ken West
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-18T23:22:29.273Z
Answers.Text: 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.
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-18T23:23:30.233Z
Answers.Text: `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.
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-18T23:40:08.173Z
Answers.Text: Updated suggested method to use `arg(0)` instead, thanks @KenWest!
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-18T23:41:29.207Z
Answers.Text: Cheers, Chris. Thanks for your answer (which I hope I marked appropriately)
Answers.UserDisplayName: Ken West
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-16T01:12:33.720Z
Answers.LastActivityDate: 2015-11-18T23:39:46.473Z
Answers.LastEditDate: 2015-11-18T23:39:46.473Z
Answers.UserDisplayName: Chris Burgess
Body: <p>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.</p>
  
  <p>'Metatag' is a Drupal module for adding  tags to the rendered HTML, and this function is in drupal/civicrm.module.</p>
  
  <p><strong>Proposed solution</strong></p>
  
  <p>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 <a href="https://github.com/civicrm/civicrm-drupal/pull/328" rel="nofollow">https://github.com/civicrm/civicrm-drupal/pull/328</a> to resolve this.</p>
  
  <p><strong>My question: How do I check that a URL is a CiviCRM URL?</strong></p>
  
  <p>As discussed below, the answer is to check if arg(0) equals 'civicrm'.</p>
  
  <p><strong>Description of problem</strong></p>
  
  <p>I get the following PHP Notice ...</p>
  
  <blockquote>
    <p>Notice: Array to string conversion in _civicrm_get_url_parameters_as_url_string() (line 295 of .../sites/all/modules/civicrm/drupal/civicrm.module).</p>
  </blockquote>
  
  <p>A page where this occurs is <a href="http://citybibleforum.org/library?&amp;title=&amp;field_speakers_tid=&amp;field_topics_tid=&amp;field_bible_reference_value=&amp;field_episode_date_value%5Bmin%5D=2011-08-12&amp;field_episode_date_value%5Bmax%5D=2012-08-12&amp;field_is_audio_value=All&amp;field_is_text_value=All&amp;field_is_video_value=All&amp;page=46" rel="nofollow">http://citybibleforum.org/library?&amp;title=&amp;field_speakers_tid=&amp;field_topics_tid=&amp;field_bible_reference_value=&amp;field_episode_date_value%5Bmin%5D=2011-08-12&amp;field_episode_date_value%5Bmax%5D=2012-08-12&amp;field_is_audio_value=All&amp;field_is_text_value=All&amp;field_is_video_value=All&amp;page=46</a></p>
  
  <p>The parameters causing the notice are ...</p>
  
  <blockquote>
    <p>field_episode_date_value[min]=2011-08-12&amp;field_episode_date_value[max]=2012-08-12</p>
  </blockquote>
  
Tags: <drupal>
Title: How can I stop civicrm_metatag_metatags_view_alter() altering non-CiviCRM URLs
ThreadText: # How can I stop civicrm_metatag_metatags_view_alter() altering non-CiviCRM URLs
  
  (Asked by: username_0 on 2015-11-16)
  
  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
  
  **Tags:** drupal
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-11-16. Score: 0 | Accepted answer
  
  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();
        }
      }  
    }
  }
  
  
  hook_metatag_metatags_view_alter()
  civicrm_metatag_metatags_view_alter()
  
  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.
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_1**: 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()`](https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/request_path/7) and [`current_path()`](https://api.drupal.org/api/drupal/includes%21path.inc/function/current_path/7). 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  
  > **username_0**: 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?  
  > **username_1**: 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.  
  > **username_1**: `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.  
  > **username_1**: Updated suggested method to use `arg(0)` instead, thanks @KenWest!  
  > **username_0**: Cheers, Chris. Thanks for your answer (which I hope I marked appropriately)  
  
  ---
  
  