Created
November 7, 2019 19:24
-
-
Save jmolivas/c12b379b642290c4f8ccfa59ab3aa1a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types = 1); | |
namespace Drupal\module_name\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; | |
/** | |
* Prepare metatag value. | |
* | |
* @ResourceFieldEnhancer( | |
* id = "metatag", | |
* label = @Translation("Metatag (Metatag field only)"), | |
* description = @Translation("Add metatag values."), | |
* dependencies = {"metatag"} | |
* ) | |
*/ | |
class MetatagEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface { | |
/** | |
* The metatag manager. | |
* | |
* @var \Drupal\metatag\MetatagManagerInterface | |
*/ | |
protected $metatagManager; | |
/** | |
* The token service. | |
* | |
* @var \Drupal\Core\Utility\Token | |
*/ | |
protected $token; | |
/** | |
* {@inheritdoc} | |
*/ | |
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; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
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') | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function doUndoTransform($data, Context $context) { | |
$field_item = $context['field_item_object']; | |
$entity = $field_item->getEntity(); | |
$metatags_for_entity = $this->metatagManager->tagsFromEntityWithDefaults($entity); | |
$token_replacements = [$entity->getEntityTypeId() => $entity]; | |
$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]); | |
} | |
} | |
return [ | |
'value' => $data, | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function doTransform($value, Context $context) { | |
return $value; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getOutputJsonSchema() { | |
return [ | |
'type' => 'object', | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment