@@ 28-73 (lines=46) @@ | ||
25 | * } |
|
26 | * ) |
|
27 | */ |
|
28 | class MenuByName extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
29 | use DependencySerializationTrait; |
|
30 | ||
31 | /** |
|
32 | * The entity type manager. |
|
33 | * |
|
34 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
35 | */ |
|
36 | protected $entityTypeManager; |
|
37 | ||
38 | /** |
|
39 | * {@inheritdoc} |
|
40 | */ |
|
41 | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
42 | return new static($configuration, $pluginId, $pluginDefinition, $container->get('entity_type.manager')); |
|
43 | } |
|
44 | ||
45 | /** |
|
46 | * MenuByName constructor. |
|
47 | * |
|
48 | * @param array $configuration |
|
49 | * The plugin configuration array. |
|
50 | * @param string $pluginId |
|
51 | * The plugin id. |
|
52 | * @param mixed $pluginDefinition |
|
53 | * The plugin definition. |
|
54 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
|
55 | * The entity type manager service. |
|
56 | */ |
|
57 | public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) { |
|
58 | parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
59 | $this->entityTypeManager = $entityTypeManager; |
|
60 | } |
|
61 | ||
62 | /** |
|
63 | * {@inheritdoc} |
|
64 | */ |
|
65 | public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
|
66 | $entity = $this->entityTypeManager->getStorage('menu')->load($args['name']); |
|
67 | ||
68 | if ($entity instanceof MenuInterface) { |
|
69 | yield $entity; |
|
70 | } |
|
71 | } |
|
72 | ||
73 | } |
|
74 |
@@ 24-81 (lines=58) @@ | ||
21 | * parents = {"ExternalUrl"} |
|
22 | * ) |
|
23 | */ |
|
24 | class ExternalRequest extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
25 | use DependencySerializationTrait; |
|
26 | ||
27 | /** |
|
28 | * @var \GuzzleHttp\ClientInterface |
|
29 | */ |
|
30 | protected $httpClient; |
|
31 | ||
32 | /** |
|
33 | * {@inheritdoc} |
|
34 | */ |
|
35 | public static function create( |
|
36 | ContainerInterface $container, |
|
37 | array $configuration, |
|
38 | $plugin_id, |
|
39 | $plugin_definition |
|
40 | ) { |
|
41 | return new static( |
|
42 | $configuration, |
|
43 | $plugin_id, |
|
44 | $plugin_definition, |
|
45 | $container->get('http_client') |
|
46 | ); |
|
47 | } |
|
48 | ||
49 | /** |
|
50 | * ExternalRequest constructor. |
|
51 | * |
|
52 | * @param array $configuration |
|
53 | * The plugin configuration array. |
|
54 | * @param string $pluginId |
|
55 | * The plugin id. |
|
56 | * @param mixed $pluginDefinition |
|
57 | * The plugin definition array. |
|
58 | * @param \GuzzleHttp\ClientInterface $httpClient |
|
59 | * The http client service. |
|
60 | */ |
|
61 | public function __construct( |
|
62 | array $configuration, |
|
63 | $pluginId, |
|
64 | $pluginDefinition, |
|
65 | ClientInterface $httpClient |
|
66 | ) { |
|
67 | parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
68 | $this->httpClient = $httpClient; |
|
69 | } |
|
70 | ||
71 | ||
72 | /** |
|
73 | * {@inheritdoc} |
|
74 | */ |
|
75 | protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
|
76 | if ($value instanceof Url) { |
|
77 | yield $this->httpClient->request('GET', $value->toString()); |
|
78 | } |
|
79 | } |
|
80 | ||
81 | } |
|
82 |
@@ 29-79 (lines=51) @@ | ||
26 | * } |
|
27 | * ) |
|
28 | */ |
|
29 | class Route extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
30 | ||
31 | /** |
|
32 | * The path validator service. |
|
33 | * |
|
34 | * @var \Drupal\Core\Path\PathValidatorInterface |
|
35 | */ |
|
36 | protected $pathValidator; |
|
37 | ||
38 | /** |
|
39 | * {@inheritdoc} |
|
40 | */ |
|
41 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
42 | return new static( |
|
43 | $configuration, |
|
44 | $plugin_id, |
|
45 | $plugin_definition, |
|
46 | $container->get('path.validator') |
|
47 | ); |
|
48 | } |
|
49 | ||
50 | /** |
|
51 | * Route constructor. |
|
52 | * |
|
53 | * @param array $configuration |
|
54 | * The plugin configuration. |
|
55 | * @param string $pluginId |
|
56 | * The plugin id. |
|
57 | * @param mixed $pluginDefinition |
|
58 | * The plugin definition. |
|
59 | * @param \Drupal\Core\Path\PathValidatorInterface $pathValidator |
|
60 | * The path validator service. |
|
61 | */ |
|
62 | public function __construct(array $configuration, $pluginId, $pluginDefinition, PathValidatorInterface $pathValidator) { |
|
63 | parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
64 | $this->pathValidator = $pathValidator; |
|
65 | } |
|
66 | ||
67 | /** |
|
68 | * {@inheritdoc} |
|
69 | */ |
|
70 | public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
|
71 | if ($url = $this->pathValidator->getUrlIfValid($args['path'])) { |
|
72 | yield $url; |
|
73 | } |
|
74 | else { |
|
75 | yield (new CacheableValue(NULL))->addCacheTags(['4xx-response']); |
|
76 | } |
|
77 | } |
|
78 | ||
79 | } |
|
80 |