Skip to content

Instantly share code, notes, and snippets.

@mortenson
Created February 26, 2019 23:29
Show Gist options
  • Save mortenson/8dc5a8c79edce7a216a7787fb2be4d48 to your computer and use it in GitHub Desktop.
Save mortenson/8dc5a8c79edce7a216a7787fb2be4d48 to your computer and use it in GitHub Desktop.
OR statements in metatags...
<?php
/**
* Implements hook_metatags_attachments_alter().
*
* This function allows you to define fallback tokens in case a field is empty.
*
* If all fallback values are empty, the tag will be empty.
*
* Example: [node:field_image:medium]||[node:field_legacy_image:medium]||/fallback.png
*/
function example_metatags_attachments_alter(array &$metatag_attachments) {
if (!empty($metatag_attachments['#attached']['html_head'])) {
$url = \Drupal::request()->getSchemeAndHttpHost();
$insecure_url = str_replace('https://', 'http://', $url);
foreach ($metatag_attachments['#attached']['html_head'] as &$attachment) {
if (isset($attachment[0]['#attributes']['content'])) {
$value = &$attachment[0]['#attributes']['content'];
}
elseif (isset($attachment[0]['#attributes']['href'])) {
$value = &$attachment[0]['#attributes']['href'];
}
if (isset($value) && strpos($value, '||') !== FALSE) {
$new_value = FALSE;
foreach (explode('||', $value) as $option) {
// We have to check against the URL because metatag will prepend our
// special tag value with the site URL if the tag plugin uses
// absolute URLs.
if (!empty($option) && $option !== $url && $option !== $insecure_url) {
// Make relative URLs absolute.
if (strpos($option, '/') === 0) {
$option = rtrim($url, '/') . $option;
}
$new_value = trim($option);
break;
}
}
$value = $new_value ?: '';
}
}
}
}
@bserem
Copy link

bserem commented Nov 25, 2020

Based on the above I created the following snippet that changes the pathauto template for an article.
Scenario:

  • AB Testing is enabled
  • An article exists in Group-B categories (a seperate vocabulary) instead of normal Group-A
  • That article should get a path alias based from Group-B vocabulary
/**        
 * Implements hook_pathauto_pattern_alter().
 * Sets path alias for articles that only exist in B group. 
 */        
function ab_pathauto_pattern_alter(\Drupal\pathauto\PathautoPatternInterface &$pattern, array $context) {
  /* @var $node Drupal\node\Entity\Node */
  if ($context['module'] !== 'node' || !($node = $context['data']['node']) || $node->bundle() !== 'article') {
    return;
  }               
                                       
  if (empty($node->get('field_categories')->getValue())) {
    // Our new pattern is almost the same, but uses the AB Testing vocabulary.
    $pattern->setPattern('[node:field_ab_testing_categories:0:entity]/[node:title]');
  }                                                                                                                                                           
}

@thejimbirch
Copy link

We had to add an is_string check to $values because sometimes $value is returned as an array and strpos can only handle strings.

-      if (isset($value) && strpos($value, '||') !== FALSE) {
+      if (isset($value) && is_string($value) && strpos($value, '||') !== FALSE) {

Credit goes to @banoodle

@thejimbirch
Copy link

PHPstan didn't like the above. Changing it to the following works and passes.

-    if (isset($value) && is_string($value) && strpos($value, '||') !== FALSE) {
+   if (is_string($value) && strpos($value, '||') !== FALSE) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment