Skip to content
Snippets Groups Projects
Commit 4dd2d8f7 authored by Patrick Kenny's avatar Patrick Kenny Committed by Björn Brala
Browse files

Issue #3483948 by ptmkenny, bbrala: Add support for attributes

parent 91300af5
No related branches found
No related tags found
1 merge request!61add attribute
Pipeline #361747 passed with warnings
<?php
namespace Drupal\jsonapi_extras\Attribute;
use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines a Plugin annotation object for resource field enhancers.
*
* @see \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class ResourceFieldEnhancer extends Plugin {
/**
* Constructs a ResourceFieldEnhancer attribute.
*
* @param string $id
* The plugin ID.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
* The human-readable name of the formatter type.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $description
* A short description of the formatter type.
* @param array $dependencies
* The name of modules that are required for this Field Enhancer to be usable.
*/
public function __construct(
public readonly string $id,
public readonly TranslatableMarkup $label,
public readonly TranslatableMarkup $description,
public readonly array $dependencies = [],
) {
}
}
...@@ -22,13 +22,18 @@ class ResourceFieldEnhancerManager extends DefaultPluginManager { ...@@ -22,13 +22,18 @@ class ResourceFieldEnhancerManager extends DefaultPluginManager {
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with. * The module handler to invoke the alter hook with.
*/ */
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { public function __construct(
\Traversable $namespaces,
CacheBackendInterface $cache_backend,
ModuleHandlerInterface $module_handler,
) {
parent::__construct( parent::__construct(
'Plugin/jsonapi/FieldEnhancer', 'Plugin/jsonapi/FieldEnhancer',
$namespaces, $namespaces,
$module_handler, $module_handler,
'Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface', 'Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface',
'Drupal\jsonapi_extras\Annotation\ResourceFieldEnhancer' 'Drupal\jsonapi_extras\Attribute\ResourceFieldEnhancer',
'Drupal\jsonapi_extras\Annotation\ResourceFieldEnhancer',
); );
$this->alterInfo('resource_field_enhancer_info'); $this->alterInfo('resource_field_enhancer_info');
......
...@@ -3,19 +3,20 @@ ...@@ -3,19 +3,20 @@
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer; namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\jsonapi_extras\Attribute\ResourceFieldEnhancer;
use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase; use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase;
use Shaper\Util\Context; use Shaper\Util\Context;
/** /**
* Perform additional manipulations to timestamp fields. * Perform additional manipulations to timestamp fields.
*
* @ResourceFieldEnhancer(
* id = "date_time",
* label = @Translation("Date Time (Timestamp field)"),
* description = @Translation("Formats a date based the configured date format for timestamp fields."),
* dependencies = {"datetime"}
* )
*/ */
#[ResourceFieldEnhancer(
id: 'date_time',
label: new TranslatableMarkup('Date Time (Timestamp field)'),
description: new TranslatableMarkup('Formats a date based the configured date format for timestamp fields.'),
dependencies: ['datetime'],
)]
class DateTimeEnhancer extends DateTimeEnhancerBase { class DateTimeEnhancer extends DateTimeEnhancerBase {
/** /**
......
...@@ -2,20 +2,21 @@ ...@@ -2,20 +2,21 @@
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer; namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\jsonapi_extras\Attribute\ResourceFieldEnhancer;
use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase; use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase;
use Shaper\Util\Context; use Shaper\Util\Context;
/** /**
* Perform additional manipulations to datetime fields. * Perform additional manipulations to datetime fields.
*
* @ResourceFieldEnhancer(
* id = "date_time_from_string",
* label = @Translation("Date Time (Date Time field)"),
* description = @Translation("Formats a date based the configured date format for date fields."),
* dependencies = {"datetime"}
* )
*/ */
#[ResourceFieldEnhancer(
id: 'date_time_from_string',
label: new TranslatableMarkup('Date Time (Date Time field)'),
description: new TranslatableMarkup('Formats a date based the configured date format for date fields.'),
dependencies: ['datetime'],
)]
class DateTimeFromStringEnhancer extends DateTimeEnhancerBase { class DateTimeFromStringEnhancer extends DateTimeEnhancerBase {
/** /**
......
...@@ -4,19 +4,20 @@ namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer; ...@@ -4,19 +4,20 @@ namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Json;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\jsonapi_extras\Attribute\ResourceFieldEnhancer;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase; use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context; use Shaper\Util\Context;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Perform additional manipulations to JSON fields. * Perform additional manipulations to JSON fields.
*
* @ResourceFieldEnhancer(
* id = "json",
* label = @Translation("JSON Field"),
* description = @Translation("Render JSON Field has real json")
* )
*/ */
#[ResourceFieldEnhancer(
id: 'json',
label: new TranslatableMarkup('JSON Field'),
description: new TranslatableMarkup('Render JSON Field has real json'),
)]
class JSONFieldEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface { class JSONFieldEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
/** /**
......
...@@ -2,19 +2,20 @@ ...@@ -2,19 +2,20 @@
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer; namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\jsonapi_extras\Attribute\ResourceFieldEnhancer;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase; use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Drupal\options\Plugin\Field\FieldType\ListItemBase; use Drupal\options\Plugin\Field\FieldType\ListItemBase;
use Shaper\Util\Context; use Shaper\Util\Context;
/** /**
* Perform additional manipulations to list fields. * Perform additional manipulations to list fields.
*
* @ResourceFieldEnhancer(
* id = "list",
* label = @Translation("List Field"),
* description = @Translation("Formats a list field based on labels and values.")
* )
*/ */
#[ResourceFieldEnhancer(
id: 'list',
label: new TranslatableMarkup('List Field'),
description: new TranslatableMarkup('Formats a list field based on labels and values.'),
)]
class ListFieldEnhancer extends ResourceFieldEnhancerBase { class ListFieldEnhancer extends ResourceFieldEnhancerBase {
/** /**
......
...@@ -2,18 +2,19 @@ ...@@ -2,18 +2,19 @@
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer; namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\jsonapi_extras\Attribute\ResourceFieldEnhancer;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase; use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context; use Shaper\Util\Context;
/** /**
* Perform additional manipulations to date fields. * Perform additional manipulations to date fields.
*
* @ResourceFieldEnhancer(
* id = "nested",
* label = @Translation("Single Nested Property"),
* description = @Translation("Extracts or wraps nested properties from an object.")
* )
*/ */
#[ResourceFieldEnhancer(
id: 'nested',
label: new TranslatableMarkup('Single Nested Property'),
description: new TranslatableMarkup('Extracts or wraps nested properties from an object.'),
)]
class SingleNestedEnhancer extends ResourceFieldEnhancerBase { class SingleNestedEnhancer extends ResourceFieldEnhancerBase {
/** /**
......
...@@ -6,7 +6,9 @@ use Drupal\Core\Language\LanguageInterface; ...@@ -6,7 +6,9 @@ use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\jsonapi_extras\Attribute\ResourceFieldEnhancer;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase; use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface; use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
use Shaper\Util\Context; use Shaper\Util\Context;
...@@ -14,13 +16,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -14,13 +16,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Add URL aliases to links. * Add URL aliases to links.
*
* @ResourceFieldEnhancer(
* id = "url_link",
* label = @Translation("URL for link (link field only)"),
* description = @Translation("Use Url for link fields.")
* )
*/ */
#[ResourceFieldEnhancer(
id: 'url_link',
label: new TranslatableMarkup('URL for link (link field only)'),
description: new TranslatableMarkup('Use Url for link fields.'),
)]
class UrlLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface { class UrlLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
/** /**
......
...@@ -4,19 +4,20 @@ namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer; ...@@ -4,19 +4,20 @@ namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\jsonapi_extras\Attribute\ResourceFieldEnhancer;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase; use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
use Shaper\Util\Context; use Shaper\Util\Context;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Use UUID for internal link field value. * Use UUID for internal link field value.
*
* @ResourceFieldEnhancer(
* id = "uuid_link",
* label = @Translation("UUID for link (link field only)"),
* description = @Translation("Use UUID for internal link field.")
* )
*/ */
#[ResourceFieldEnhancer(
id: 'uuid_link',
label: new TranslatableMarkup('UUID for link (link field only)'),
description: new TranslatableMarkup('Use UUID for internal link field.'),
)]
class UuidLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface { class UuidLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFactoryPluginInterface {
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment