diff --git a/token.tokens.inc b/token.tokens.inc index f38955a..1496954 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -20,6 +20,7 @@ use Drupal\menu_link_content\MenuLinkContentInterface; use Drupal\system\Entity\Menu; use Drupal\user\UserInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface; +use Drupal\image\Entity\ImageStyle; /** * Implements hook_token_info_alter(). @@ -433,6 +434,44 @@ function token_tokens($type, array $tokens, array $data = array(), array $option $node = $data['node']; foreach ($tokens as $name => $original) { + // Field image tokens. Integration of Drupal 7's Imagecache Token module. + $parts = explode(':', $name); + if ((count($parts) === 3) && $parts[0] === 'field_image') { + // Token is of the form field_image:{image_style}:type. An image + // derivative of the original has to be created first. + /** @var \Drupal\file\Entity\File $field_image */ + $field_image = $node->field_image->entity; + $original_uri = $field_image->getFileUri(); + $style = ImageStyle::load($parts[1]); + $derivative_uri = $style->buildUri($original_uri); + + if ($style->createDerivative($original_uri, $derivative_uri)) { + /** @var \Drupal\Core\Image\Image $image */ + $image = Drupal::service('image.factory')->get($derivative_uri); + + // Generate the replacement token. + switch ($parts[2]) { + case 'mimetype': + $replacements[$original] = $image->getMimeType(); + break; + case 'filesize' : + $replacements[$original] = $image->getFileSize(); + break; + case 'height' : + $replacements[$original] = $image->getHeight(); + break; + case 'width' : + $replacements[$original] = $image->getWidth(); + break; + case 'uri' : + $replacements[$original] = $derivative_uri; + break; + } + } + continue; + } + + // Other tokens. switch ($name) { case 'log': $replacements[$original] = (string) $node->revision_log->value; @@ -1230,6 +1269,48 @@ function field_token_info_alter(&$info) { ); } } + + // Provide additional tokens for image_style. Integration of Drupal 7's + // Imagecache Token module. + $info['tokens']['image_style']['mimetype'] = [ + 'name' => t('MIME type'), + 'description' => t('The MIME type (image/png, image/bmp, etc.) of the image.'), + ]; + $info['tokens']['image_style']['filesize'] = [ + 'name' => t('File size'), + 'description' => t('The file size of the image.'), + ]; + $info['tokens']['image_style']['height'] = [ + 'name' => t('Height'), + 'description' => t('The height the image, in pixels.'), + ]; + $info['tokens']['image_style']['width'] = [ + 'name' => t('Width'), + 'description' => t('The width of the image, in pixels.'), + ]; + $info['tokens']['image_style']['uri'] = [ + 'name' => t('URI'), + 'description' => t('The URI to the image.'), + ]; + + // Provide image_field tokens for the different image styles. + $info['types']['field_image'] = [ + 'name' => t('Image field'), + 'needs-data' => 'field_image', + 'module' => 'token', + ]; + + $image_styles = image_style_options(FALSE); + foreach ($image_styles as $style => $description) { + $info['tokens']['field_image'][$style] = [ + 'name' => $style, + 'description' => t('@description image.', ['@description' => $description]), + 'type' => 'image_style', + ]; + } + + // Change the existing field_image token type to put everything together. + $info['tokens']['node']['field_image']['type'] = 'field_image'; } /**