Created
October 29, 2019 21:06
-
-
Save jmolivas/19f2dd15e73a5fdd93dc85edfd1db845 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 | |
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