X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Ftoken%2Ftoken.tokens.inc;fp=web%2Fmodules%2Fcontrib%2Ftoken%2Ftoken.tokens.inc;h=950f0ed06b08f8b3eb400fede59057b524d59810;hp=f6ad619109dfa0c8f13b7e8e9eb5484b44324796;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/modules/contrib/token/token.tokens.inc b/web/modules/contrib/token/token.tokens.inc index f6ad61910..950f0ed06 100644 --- a/web/modules/contrib/token/token.tokens.inc +++ b/web/modules/contrib/token/token.tokens.inc @@ -114,7 +114,7 @@ function token_token_info_alter(&$info) { if (!isset($info['tokens']['date'][$date_format_type])) { $info['tokens']['date'][$date_format_type] = array( 'name' => Html::escape($date_format_type_info->label()), - 'description' => t("A date in '@type' format. (%date)", array('@type' => $date_format_type, '%date' => format_date(REQUEST_TIME, $date_format_type))), + 'description' => t("A date in '@type' format. (%date)", array('@type' => $date_format_type, '%date' => \Drupal::service('date.formatter')->format(REQUEST_TIME, $date_format_type))), 'module' => 'token', ); } @@ -447,7 +447,7 @@ function token_tokens($type, array $tokens, array $data = array(), array $option $date_format_types = \Drupal::entityTypeManager()->getStorage('date_format')->loadMultiple(); foreach ($tokens as $name => $original) { if (isset($date_format_types[$name]) && _token_module('date', $name) == 'token') { - $replacements[$original] = format_date($date, $name, '', NULL, $langcode); + $replacements[$original] = \Drupal::service('date.formatter')->format($date, $name, '', NULL, $langcode); } } } @@ -795,7 +795,11 @@ function token_tokens($type, array $tokens, array $data = array(), array $option $url = $data['url']; // To retrieve the correct path, modify a copy of the Url object. $path_url = clone $url; - $path = '/' . $path_url->setAbsolute(FALSE)->setOption('fragment', NULL)->getInternalPath(); + $path = '/'; + // Ensure the URL is routed to avoid throwing an exception. + if ($url->isRouted()) { + $path .= $path_url->setAbsolute(FALSE)->setOption('fragment', NULL)->getInternalPath(); + } foreach ($tokens as $name => $original) { switch ($name) { @@ -1318,17 +1322,13 @@ function field_token_info_alter(&$info) { // Make sure a token type exists for this entity. $token_type = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entity_type_id); - if (empty($token_type)) { + if (empty($token_type) || !isset($info['types'][$token_type])) { continue; } $fields = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id); foreach ($fields as $field_name => $field) { /** @var \Drupal\field\FieldStorageConfigInterface $field */ - // Ensure the token type exists. - if (!isset($info['types'][$token_type])) { - continue; - } // Ensure the token implements FieldStorageConfigInterface or is defined // in token module. @@ -1639,52 +1639,56 @@ function field_tokens($type, $tokens, array $data = array(), array $options = ar // replacement. $property_name = isset($parts[1]) ? $parts[1] : 'url'; $entity = $field_item->entity; - $original_uri = $entity->getFileUri(); - - // Only generate the image derivative if needed. - if ($property_name === 'width' || $property_name === 'height') { - $dimensions = [ - 'width' => $field_item->width, - 'height' => $field_item->height, - ]; - $style->transformDimensions($dimensions, $original_uri); - $replacements[$original] = $dimensions[$property_name]; - } - elseif ($property_name === 'uri') { - $replacements[$original] = $style->buildUri($original_uri); - } - elseif ($property_name === 'url') { - $replacements[$original] = $style->buildUrl($original_uri); - } - else { - // Generate the image derivative, if it doesn't already exist. - $derivative_uri = $style->buildUri($original_uri); - $derivative_exists = TRUE; - if (!file_exists($derivative_uri)) { - $derivative_exists = $style->createDerivative($original_uri, $derivative_uri); + if (!empty($field_item->entity)) { + $original_uri = $entity->getFileUri(); + + // Only generate the image derivative if needed. + if ($property_name === 'width' || $property_name === 'height') { + $dimensions = [ + 'width' => $field_item->width, + 'height' => $field_item->height, + ]; + $style->transformDimensions($dimensions, $original_uri); + $replacements[$original] = $dimensions[$property_name]; + } + elseif ($property_name === 'uri') { + $replacements[$original] = $style->buildUri($original_uri); } - if ($derivative_exists) { - $image = \Drupal::service('image.factory')->get($derivative_uri); - // Provide the replacement. - switch ($property_name) { - case 'mimetype': - $replacements[$original] = $image->getMimeType(); - break; - case 'filesize' : - $replacements[$original] = $image->getFileSize(); - break; + elseif ($property_name === 'url') { + $replacements[$original] = $style->buildUrl($original_uri); + } + else { + // Generate the image derivative, if it doesn't already exist. + $derivative_uri = $style->buildUri($original_uri); + $derivative_exists = TRUE; + if (!file_exists($derivative_uri)) { + $derivative_exists = $style->createDerivative($original_uri, $derivative_uri); + } + if ($derivative_exists) { + $image = \Drupal::service('image.factory')->get($derivative_uri); + // Provide the replacement. + switch ($property_name) { + case 'mimetype': + $replacements[$original] = $image->getMimeType(); + break; + case 'filesize' : + $replacements[$original] = $image->getFileSize(); + break; + } } } } } elseif (in_array($field_item->getFieldDefinition()->getType(), ['datetime', 'daterange']) && in_array($property_name, ['date', 'start_date', 'end_date'])) { - $datetime = $field_item->$property_name->getTimestamp(); - if($property_name == $token) { - $replacements[$original] = $datetime; + $timestamp = $field_item->$property_name->getTimestamp(); + // If the token is an exact match for the property or the delta and the + // property, use the timestamp as-is. + if($property_name == $token || "$delta:$property_name" == $token) { + $replacements[$original] = $timestamp; } else { - $field_tokens = \Drupal::token()->findWithPrefix($tokens, $property_name); - $replacements += \Drupal::token()->generate('date', $field_tokens, ['date' => $datetime], $options, $bubbleable_metadata); + $date_tokens = \Drupal::token()->findWithPrefix($filtered_tokens, $property_name); + $replacements += \Drupal::token()->generate('date', $date_tokens, ['date' => $timestamp], $options, $bubbleable_metadata); } } else {