Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace Drupal\module_name\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\Url;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context;
/**
* Use alias for internal link field value.
*
* @ResourceFieldEnhancer(
* id = "alias_link",
* label = @Translation("Alias for link (link field only)"),
* description = @Translation("Add alias for internal link field.")
* )
*/
class AliasLinkEnhancer extends ResourceFieldEnhancerBase {
/**
* {@inheritdoc}
*/
protected function doUndoTransform($data, Context $context) {
if (isset($data['uri'])) {
$url = Url::fromUri($data['uri']);
$data['alias'] = $url->toString(TRUE)->getGeneratedUrl();
}
return $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