View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share\Plugin\jsonapi\FieldEnhancer;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Token;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Drupal\metatag\MetatagManagerInterface;
use Shaper\Util\Context;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityShareMetatagEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
protected $metatagManager;
protected $token;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, MetatagManagerInterface $metatag_manager, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->metatagManager = $metatag_manager;
$this->token = $token;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('metatag.manager'), $container
->get('token'));
}
protected function doUndoTransform($data, Context $context) {
$configuration = $this
->getConfiguration();
if ($configuration['expose_default_tags']) {
$field_item = $context['field_item_object'];
$entity = $field_item
->getEntity();
$metatags_for_entity = $this->metatagManager
->tagsFromEntityWithDefaults($entity);
if ($configuration['replace_tokens']) {
$token_replacements = [
$entity
->getEntityTypeId() => $entity,
];
$replacements_options = [];
if ($configuration['clear_tokens']) {
$replacements_options['clear'] = TRUE;
}
$data = [];
foreach ($metatags_for_entity as $metatag_key => $metatag_for_entity) {
$data[$metatag_key] = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->token
->replace($metatag_for_entity, $token_replacements, $replacements_options)));
if (empty($data[$metatag_key])) {
unset($data[$metatag_key]);
}
}
}
else {
$data = $metatags_for_entity;
}
}
return [
'value' => $data,
];
}
protected function doTransform($value, Context $context) {
return $value;
}
public function getOutputJsonSchema() {
return [
'type' => 'object',
];
}
public function defaultConfiguration() {
return [
'expose_default_tags' => TRUE,
'replace_tokens' => TRUE,
'clear_tokens' => TRUE,
];
}
public function getSettingsForm(array $resource_field_info) {
$settings = empty($resource_field_info['enhancer']['settings']) ? $this
->getConfiguration() : $resource_field_info['enhancer']['settings'];
return [
'expose_default_tags' => [
'#type' => 'checkbox',
'#title' => $this
->t('Expose default tags'),
'#description' => $this
->t('Expose tags that have a default value, usually with tokens, and are not overridden in the entity.'),
'#default_value' => $settings['expose_default_tags'],
],
'replace_tokens' => [
'#type' => 'checkbox',
'#title' => $this
->t('Replace tokens'),
'#description' => $this
->t('Replace tokens by its value.'),
'#default_value' => $settings['replace_tokens'],
'#states' => [
'visible' => [
':input[name="resourceFields[' . $resource_field_info['fieldName'] . '][enhancer][settings][expose_default_tags]"]' => [
'checked' => TRUE,
],
],
],
],
'clear_tokens' => [
'#type' => 'checkbox',
'#title' => $this
->t('Clear tokens'),
'#description' => $this
->t('Remove tokens from the final text if no replacement value can be generated.'),
'#default_value' => $settings['clear_tokens'],
'#states' => [
'visible' => [
':input[name="resourceFields[' . $resource_field_info['fieldName'] . '][enhancer][settings][expose_default_tags]"]' => [
'checked' => TRUE,
],
':input[name="resourceFields[' . $resource_field_info['fieldName'] . '][enhancer][settings][replace_tokens]"]' => [
'checked' => TRUE,
],
],
],
],
];
}
}