We have a custom module mymodule
where we call mymodule_tokens
so that we can assign custom token values from the current node. These custom tokens are used to insert values in the Article's MetaTag fields.
Problem
When fetching the default value based on an Image Style (e.g. medium), I cannot get the Height and Width based on the Configuration for the Image Style. How can I get the height and width from the Image Style?
What I have tried, but cannot get the height and width:
function mymodule_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
$node = $data['node'];
$node_image = $node->field_image;
$field_image_style = 'medium';
if($node_image->isEmpty()) {
$field_info = FieldConfig::loadByName('node', 'article, 'field_image');
$image_uuid = $field_info->getSetting('default_image')['uuid'];
$image = Drupal::service('entity.repository')->loadEntityByUuid('file', $image_uuid);
$field_image_url = ImageStyle::load($field_image_style)->buildUrl($image->getFileUri());
$image_style = ImageStyle::load($field_image_style)->getEffects()->getConfiguration();
// Both give me error messages.
$image_style_height = $image_style->get('height');
$image_style_width = $image_style->get('width');
}
else {
$field_image_url = $node_field_image->entity->getFileUri();
$image_style = ImageStyle::load($field_image_style)->getEffects()->getConfiguration();
// Both give me error messages.
$image_style_height = $image_style->get('height');
$image_style_width = $image_style->get('width');
}
foreach($tokens as $name => $original) {
switch ($name) {
case 'custom_image_url':
$replacements[$original] = $field_image_url;
break;
case 'custom_image_height':
$replacements[$original] = $image_style_height;
break;
case 'custom_image_width':
$replacements[$original] = $image_style_width;
break;
}
}
return $replacements;
}
Error Message
Error: Call to a member function get() on array in mymodule_tokens()
When printing ImageStyle::load($field_image_style)
using Kint this is what we get.
When printing $image_style
, I get the following.
Using core or contrib tokens rather than custom tokens
Using the following tokens only work when there is a value added to the field_image
field, but when the field_image
is not populated, the token is not able to return the default image that is populated on the field's settings page.
[node:field_image:medium:height]
[node:field_image:medium:url]
[node:field_image:medium:width]
ksm($image_style)
to show you the available methods? I doubt that the$image_style
object holds the finalized image properties.kint()
(same output when usingksm()
). I can see that in the'data'
array the width and height are there. I wonder if it has to do with being protected.medium
performs a scale and crop wherewidth => 400
andheight => 260
. I wanted to query that setting and use it to assign values to the custom tokens (see updated code) rather than hard coding the400
and260
in case those values are changed. Tokens such as[node:field_image:medium:height]
does not return a value when thefield_image
is empty, hence the custom tokens.