Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Created October 29, 2019 21:06
Show Gist options
  • Save jmolivas/19f2dd15e73a5fdd93dc85edfd1db845 to your computer and use it in GitHub Desktop.
Save jmolivas/19f2dd15e73a5fdd93dc85edfd1db845 to your computer and use it in GitHub Desktop.
<?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