@amazeelabs/silverback-gatsby 3.7.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/drupal/silverback_gatsby/.prettierignore +1 -0
  3. package/drupal/silverback_gatsby/CHANGELOG.md +1216 -0
  4. package/drupal/silverback_gatsby/README.md +297 -0
  5. package/drupal/silverback_gatsby/composer.json +15 -0
  6. package/drupal/silverback_gatsby/directives.gql +63 -0
  7. package/drupal/silverback_gatsby/directives.graphql +62 -0
  8. package/drupal/silverback_gatsby/drush.services.yml +9 -0
  9. package/drupal/silverback_gatsby/graphql/entity.directive.graphqls +2 -0
  10. package/drupal/silverback_gatsby/graphql/menu.directive.graphqls +17 -0
  11. package/drupal/silverback_gatsby/graphql/silverback_gatsby.base.graphqls +25 -0
  12. package/drupal/silverback_gatsby/graphql/silverback_gatsby.extension.graphqls +10 -0
  13. package/drupal/silverback_gatsby/graphql/stringTranslation.directive.graphqls +4 -0
  14. package/drupal/silverback_gatsby/graphql/translatableString.directive.graphqls +2 -0
  15. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/directives.gql +6 -0
  16. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/graphql/.graphqlrc.json +4 -0
  17. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/graphql/silverback_gatsby_example.graphqls +39 -0
  18. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/silverback_gatsby_example.info.yml +7 -0
  19. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/silverback_gatsby_example.module +16 -0
  20. package/drupal/silverback_gatsby/modules/silverback_gatsby_example/src/Directives.php +24 -0
  21. package/drupal/silverback_gatsby/silverback_gatsby.info.yml +8 -0
  22. package/drupal/silverback_gatsby/silverback_gatsby.install +180 -0
  23. package/drupal/silverback_gatsby/silverback_gatsby.links.task.yml +6 -0
  24. package/drupal/silverback_gatsby/silverback_gatsby.module +119 -0
  25. package/drupal/silverback_gatsby/silverback_gatsby.permissions.yml +8 -0
  26. package/drupal/silverback_gatsby/silverback_gatsby.post_update.php +15 -0
  27. package/drupal/silverback_gatsby/silverback_gatsby.routing.yml +28 -0
  28. package/drupal/silverback_gatsby/silverback_gatsby.services.yml +50 -0
  29. package/drupal/silverback_gatsby/src/Annotation/GatsbyFeed.php +41 -0
  30. package/drupal/silverback_gatsby/src/Commands/SilverbackGatsbyCommands.php +102 -0
  31. package/drupal/silverback_gatsby/src/Controller/BuildController.php +45 -0
  32. package/drupal/silverback_gatsby/src/Controller/PublisherController.php +34 -0
  33. package/drupal/silverback_gatsby/src/Directives.php +51 -0
  34. package/drupal/silverback_gatsby/src/GatsbyBuildTrigger.php +230 -0
  35. package/drupal/silverback_gatsby/src/GatsbyBuildTriggerInterface.php +52 -0
  36. package/drupal/silverback_gatsby/src/GatsbyUpdate.php +38 -0
  37. package/drupal/silverback_gatsby/src/GatsbyUpdateHandler.php +181 -0
  38. package/drupal/silverback_gatsby/src/GatsbyUpdateTracker.php +98 -0
  39. package/drupal/silverback_gatsby/src/GatsbyUpdateTrackerInterface.php +59 -0
  40. package/drupal/silverback_gatsby/src/GatsbyUpdateTrigger.php +69 -0
  41. package/drupal/silverback_gatsby/src/GatsbyUpdateTriggerInterface.php +22 -0
  42. package/drupal/silverback_gatsby/src/GraphQL/Build.php +235 -0
  43. package/drupal/silverback_gatsby/src/GraphQL/ComposableSchema.php +73 -0
  44. package/drupal/silverback_gatsby/src/LocaleStorageDecorator.php +181 -0
  45. package/drupal/silverback_gatsby/src/MenuTreeStorageDecorator.php +139 -0
  46. package/drupal/silverback_gatsby/src/Plugin/FeedBase.php +121 -0
  47. package/drupal/silverback_gatsby/src/Plugin/FeedInterface.php +132 -0
  48. package/drupal/silverback_gatsby/src/Plugin/FeedPluginManager.php +39 -0
  49. package/drupal/silverback_gatsby/src/Plugin/Gatsby/Feed/EntityFeed.php +252 -0
  50. package/drupal/silverback_gatsby/src/Plugin/Gatsby/Feed/MenuFeed.php +292 -0
  51. package/drupal/silverback_gatsby/src/Plugin/Gatsby/Feed/StringTranslationFeed.php +171 -0
  52. package/drupal/silverback_gatsby/src/Plugin/Gatsby/Feed/TranslatableStringFeed.php +128 -0
  53. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/CurrentUserEntity.php +43 -0
  54. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/EntityQueryBase.php +40 -0
  55. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/FetchEntity.php +371 -0
  56. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/FetchString.php +64 -0
  57. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/FetchTranslatableString.php +74 -0
  58. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/FocalPoint.php +47 -0
  59. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/GatsbyBuildId.php +36 -0
  60. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/GatsbyExtractId.php +28 -0
  61. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/GatsbyExtractLangcode.php +28 -0
  62. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/ImageProps.php +149 -0
  63. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/ListEntities.php +88 -0
  64. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/ListStrings.php +87 -0
  65. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/StringId.php +37 -0
  66. package/drupal/silverback_gatsby/src/Plugin/GraphQL/DataProducer/StringTranslations.php +94 -0
  67. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/EntityFetch.php +51 -0
  68. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/EntityTranslationsWithDefault.php +30 -0
  69. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/FocalPoint.php +27 -0
  70. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/ImageProps.php +27 -0
  71. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/MenuLangcode.php +27 -0
  72. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/MenuTranslations.php +36 -0
  73. package/drupal/silverback_gatsby/src/Plugin/GraphQL/Directive/SilverbackGatsbyEntityId.php +39 -0
  74. package/drupal/silverback_gatsby/src/Plugin/GraphQL/SchemaExtension/SilverbackGatsbySchemaExtension.php +384 -0
  75. package/drupal/silverback_gatsby/src/SilverbackGatsbyServiceProvider.php +43 -0
  76. package/drupal/silverback_gatsby/src/SilverbackGatsbySessionConfiguration.php +30 -0
  77. package/drupal/silverback_gatsby/src/SilverbackReverseProxyMiddleware.php +49 -0
  78. package/drupal/silverback_gatsby/tests/queries/create-page-fields.gql +12 -0
  79. package/drupal/silverback_gatsby/tests/queries/current-user.gql +6 -0
  80. package/drupal/silverback_gatsby/tests/queries/feed_info.gql +12 -0
  81. package/drupal/silverback_gatsby/tests/queries/load-entity.gql +5 -0
  82. package/drupal/silverback_gatsby/tests/queries/menus.gql +31 -0
  83. package/drupal/silverback_gatsby/tests/queries/multilingual-menus.gql +11 -0
  84. package/drupal/silverback_gatsby/tests/queries/revisionable-translatable.gql +15 -0
  85. package/drupal/silverback_gatsby/tests/queries/revisionable.gql +8 -0
  86. package/drupal/silverback_gatsby/tests/queries/translatable.gql +14 -0
  87. package/drupal/silverback_gatsby/tests/queries/untranslatable.gql +10 -0
  88. package/drupal/silverback_gatsby/tests/schema/.graphqlrc.json +4 -0
  89. package/drupal/silverback_gatsby/tests/schema/translatable-strings.graphql +7 -0
  90. package/drupal/silverback_gatsby/tests/src/Kernel/CurrentUserTest.php +37 -0
  91. package/drupal/silverback_gatsby/tests/src/Kernel/EntityFeedTest.php +490 -0
  92. package/drupal/silverback_gatsby/tests/src/Kernel/EntityFeedTestBase.php +178 -0
  93. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyBuildTriggerTest.php +155 -0
  94. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyFeedInfoTest.php +319 -0
  95. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyUpdateHandlerTest.php +131 -0
  96. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyUpdateTrackerTest.php +196 -0
  97. package/drupal/silverback_gatsby/tests/src/Kernel/GatsbyUpdateTriggerTest.php +177 -0
  98. package/drupal/silverback_gatsby/tests/src/Kernel/MenuFeedTest.php +192 -0
  99. package/drupal/silverback_gatsby/tests/src/Kernel/TranslatableStringFeedTest.php +210 -0
  100. package/drupal/silverback_gatsby/tests/src/Traits/NotificationCheckTrait.php +43 -0
  101. package/package.json +15 -0
@@ -0,0 +1,149 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\DataProducer;
4
+
5
+ use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
6
+ use Drupal\Core\File\FileUrlGeneratorInterface;
7
+ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
+ use Drupal\Core\Render\RenderContext;
9
+ use Drupal\Core\Render\RendererInterface;
10
+ use Drupal\file\FileInterface;
11
+ use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
12
+ use Drupal\image\Entity\ImageStyle;
13
+ use Symfony\Component\DependencyInjection\ContainerInterface;
14
+
15
+ /**
16
+ * Returns an image style derivative of an image.
17
+ *
18
+ * @DataProducer(
19
+ * id = "image_props",
20
+ * name = @Translation("Image properties"),
21
+ * description = @Translation("Returns an image's properties."),
22
+ * produces = @ContextDefinition("any",
23
+ * label = @Translation("Image properties")
24
+ * ),
25
+ * consumes = {
26
+ * "entity" = @ContextDefinition("entity",
27
+ * label = @Translation("Entity"),
28
+ * required = FALSE
29
+ * )
30
+ * }
31
+ * )
32
+ */
33
+ class ImageProps extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
34
+
35
+ /**
36
+ * The rendering service.
37
+ *
38
+ * @var \Drupal\Core\Render\RendererInterface
39
+ */
40
+ protected $renderer;
41
+
42
+ /**
43
+ * The file URL generator service.
44
+ *
45
+ * @var \Drupal\Core\File\FileUrlGeneratorInterface
46
+ */
47
+ protected $fileUrlGenerator;
48
+
49
+ /**
50
+ * {@inheritdoc}
51
+ *
52
+ * @codeCoverageIgnore
53
+ */
54
+ public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
55
+ return new static(
56
+ $configuration,
57
+ $pluginId,
58
+ $pluginDefinition,
59
+ $container->get('renderer'),
60
+ $container->get('file_url_generator')
61
+ );
62
+ }
63
+
64
+ /**
65
+ * ImageProps constructor.
66
+ *
67
+ * @param array $configuration
68
+ * The plugin configuration array.
69
+ * @param string $pluginId
70
+ * The plugin id.
71
+ * @param mixed $pluginDefinition
72
+ * The plugin definition.
73
+ * @param \Drupal\Core\Render\RendererInterface $renderer
74
+ * The renderer service.
75
+ * @param \Drupal\Core\File\FileUrlGeneratorInterface $fileGenerator
76
+ * The file generator service.
77
+ *
78
+ * @codeCoverageIgnore
79
+ */
80
+ public function __construct(
81
+ array $configuration,
82
+ $pluginId,
83
+ $pluginDefinition,
84
+ RendererInterface $renderer,
85
+ FileUrlGeneratorInterface $fileGenerator
86
+ ) {
87
+ parent::__construct($configuration, $pluginId, $pluginDefinition);
88
+ $this->renderer = $renderer;
89
+ $this->fileUrlGenerator = $fileGenerator;
90
+ }
91
+
92
+ /**
93
+ * Resolver.
94
+ *
95
+ * @param \Drupal\file\FileInterface $entity
96
+ * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
97
+ *
98
+ * @return array|null
99
+ */
100
+ public function resolve(?FileInterface $entity, RefinableCacheableDependencyInterface $metadata) {
101
+ // Return if we dont have an entity.
102
+ if (!$entity) {
103
+ return NULL;
104
+ }
105
+
106
+ $access = $entity->access('view', NULL, TRUE);
107
+ $metadata->addCacheableDependency($access);
108
+ if ($access->isAllowed()) {
109
+
110
+ $width = $entity->width;
111
+ $height = $entity->height;
112
+
113
+ // @todo Not sure why PHPStan complains here, this should be refactored to
114
+ // check the entity properties first.
115
+ // @phpstan-ignore-next-line
116
+ if (empty($width) || empty($height)) {
117
+ /** @var \Drupal\Core\Image\ImageInterface $image */
118
+ $image = \Drupal::service('image.factory')->get($entity->getFileUri());
119
+ if ($image->isValid()) {
120
+ $width = $image->getWidth();
121
+ $height = $image->getHeight();
122
+ }
123
+ }
124
+
125
+
126
+ // The underlying URL generator that will be invoked will leak cache
127
+ // metadata, resulting in an exception. By wrapping within a new render
128
+ // context, we can capture the leaked metadata and make sure it gets
129
+ // incorporated into the response.
130
+ $context = new RenderContext();
131
+ $url = $this->renderer->executeInRenderContext($context, function () use ($entity) {
132
+ return $this->fileUrlGenerator->generateAbsoluteString($entity->getFileUri());
133
+ });
134
+
135
+ if (!$context->isEmpty()) {
136
+ $metadata->addCacheableDependency($context->pop());
137
+ }
138
+
139
+ return [
140
+ 'src' => $url,
141
+ 'width' => $width,
142
+ 'height' => $height,
143
+ ];
144
+ }
145
+
146
+ return NULL;
147
+ }
148
+
149
+ }
@@ -0,0 +1,88 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\DataProducer;
4
+
5
+ use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
6
+ use Drupal\Core\Entity\EntityInterface;
7
+ use Drupal\Core\Entity\TranslatableInterface;
8
+
9
+ /**
10
+ * @DataProducer(
11
+ * id = "list_entities",
12
+ * name = @Translation("List entities"),
13
+ * produces = @ContextDefinition("entity",
14
+ * label = @Translation("Entities"),
15
+ * multiple = TRUE,
16
+ * ),
17
+ * consumes = {
18
+ * "type" = @ContextDefinition("string",
19
+ * label = @Translation("Entity type"),
20
+ * required = TRUE,
21
+ * ),
22
+ * "bundle" = @ContextDefinition("string",
23
+ * label = @Translation("Bundle"),
24
+ * required = FALSE,
25
+ * ),
26
+ * "offset" = @ContextDefinition("integer",
27
+ * label = @Translation("Offset"),
28
+ * required = FALSE,
29
+ * ),
30
+ * "limit" = @ContextDefinition("integer",
31
+ * label = @Translation("Limit"),
32
+ * required = FALSE,
33
+ * ),
34
+ * "access" = @ContextDefinition("boolean",
35
+ * label = @Translation("Whether to do additional access check"),
36
+ * required = FALSE,
37
+ * default_value = TRUE,
38
+ * ),
39
+ * },
40
+ * )
41
+ */
42
+ class ListEntities extends EntityQueryBase {
43
+
44
+ public function resolve(string $type, ?string $bundle, ?int $offset, ?int $limit, $access, RefinableCacheableDependencyInterface $metadata) {
45
+ $storage = \Drupal::entityTypeManager()->getStorage($type);
46
+ $entityType = $storage->getEntityType();
47
+ $query = $this->getQuery($type, $metadata);
48
+
49
+ if ($bundle) {
50
+ $query->condition($entityType->getKey('bundle'), $bundle);
51
+ }
52
+ $query->range($offset, $limit);
53
+
54
+ $ids = $query->execute();
55
+ $entities = !empty($ids) ? $storage->loadMultiple($ids) : [];
56
+
57
+ $metadata->addCacheTags($entityType->getListCacheTags());
58
+ $metadata->addCacheContexts($entityType->getListCacheContexts());
59
+
60
+ $entities = $access
61
+ ? array_map(
62
+ function (EntityInterface $entity) {
63
+ $result = null;
64
+ if ($entity->access('view')) {
65
+ $result = $entity;
66
+ }
67
+ else if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {
68
+ foreach ($entity->getTranslationLanguages(false) as $lang) {
69
+ $translation = $entity->getTranslation($lang->getId());
70
+ if ($translation->access('view')) {
71
+ $result = $translation;
72
+ break;
73
+ }
74
+ }
75
+ }
76
+ return $result;
77
+ },
78
+ $entities
79
+ ) : $entities;
80
+
81
+ foreach (array_filter($entities) as $entity) {
82
+ $metadata->addCacheableDependency($entity);
83
+ }
84
+
85
+ return $entities;
86
+ }
87
+
88
+ }
@@ -0,0 +1,87 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\DataProducer;
4
+
5
+ use Drupal\Core\Database\Connection;
6
+ use Drupal\Core\Database\Query\Condition;
7
+ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
+ use Drupal\graphql\GraphQL\Execution\FieldContext;
9
+ use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
10
+ use Drupal\locale\SourceString;
11
+ use Symfony\Component\DependencyInjection\ContainerInterface;
12
+
13
+ /**
14
+ * @DataProducer(
15
+ * id = "list_strings",
16
+ * name = @Translation("List strings"),
17
+ * produces = @ContextDefinition("any",
18
+ * label = @Translation("Strings"),
19
+ * multiple = TRUE,
20
+ * ),
21
+ * consumes = {
22
+ * "offset" = @ContextDefinition("integer",
23
+ * label = @Translation("Offset"),
24
+ * required = FALSE,
25
+ * ),
26
+ * "limit" = @ContextDefinition("integer",
27
+ * label = @Translation("Limit"),
28
+ * required = FALSE,
29
+ * ),
30
+ * "translationContext" = @ContextDefinition("string",
31
+ * label = @Translation("Translation context"),
32
+ * multiple = TRUE,
33
+ * required = FALSE,
34
+ * ),
35
+ * },
36
+ * )
37
+ */
38
+ class ListStrings extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
39
+
40
+ /**
41
+ * The locale storage service.
42
+ *
43
+ * @var \Drupal\Core\Database\Connection
44
+ */
45
+ protected $connection;
46
+
47
+ public function __construct(
48
+ array $configuration,
49
+ $plugin_id,
50
+ $plugin_definition,
51
+ Connection $connection
52
+ ) {
53
+ parent::__construct($configuration, $plugin_id, $plugin_definition);
54
+ $this->connection = $connection;
55
+ }
56
+
57
+ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
58
+ return new static(
59
+ $configuration,
60
+ $plugin_id,
61
+ $plugin_definition,
62
+ $container->get('database'),
63
+ );
64
+ }
65
+
66
+ public function resolve(int $offset, int $limit, array $translationContext, FieldContext $context) {
67
+ $context->addCacheTags(['locale']);
68
+ $query = $this->connection->select('locales_source', 's')
69
+ ->fields('s');
70
+ if (!empty($translationContext)) {
71
+ $condition = new Condition('OR');
72
+ foreach ($translationContext as $item) {
73
+ $condition->condition('s.context', $this->connection->escapeLike($item) . '%', 'LIKE');
74
+ }
75
+ $query->condition($condition);
76
+ }
77
+ $query->range($offset, $limit);
78
+ $result = $query->execute()->fetchAll();
79
+ $strings = [];
80
+ foreach ($result as $item) {
81
+ $string = new SourceString($item);
82
+ $strings[$string->getId()] = $string;
83
+ }
84
+
85
+ return $strings;
86
+ }
87
+ }
@@ -0,0 +1,37 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\DataProducer;
4
+
5
+ use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
6
+ use Drupal\locale\StringInterface;
7
+
8
+ /**
9
+ * Returns the ID of a string.
10
+ *
11
+ * @DataProducer(
12
+ * id = "string_id",
13
+ * name = @Translation("String identifier"),
14
+ * description = @Translation("Returns the string identifier."),
15
+ * produces = @ContextDefinition("string",
16
+ * label = @Translation("Identifier")
17
+ * ),
18
+ * consumes = {
19
+ * "string" = @ContextDefinition("any",
20
+ * label = @Translation("String")
21
+ * )
22
+ * }
23
+ * )
24
+ */
25
+ class StringId extends DataProducerPluginBase {
26
+
27
+ /**
28
+ * Resolver.
29
+ *
30
+ * @param \Drupal\locale\StringInterface $string
31
+ *
32
+ * @return int|string|null
33
+ */
34
+ public function resolve(StringInterface $string) {
35
+ return $string->getId();
36
+ }
37
+ }
@@ -0,0 +1,94 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\DataProducer;
4
+
5
+ use Drupal\Core\Language\LanguageManagerInterface;
6
+ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
+ use Drupal\graphql\GraphQL\Execution\FieldContext;
8
+ use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
9
+ use Drupal\locale\StringInterface;
10
+ use Drupal\locale\StringStorageInterface;
11
+ use Drupal\locale\TranslationString;
12
+ use Symfony\Component\DependencyInjection\ContainerInterface;
13
+
14
+ /**
15
+ * Returns the translations of a string.
16
+ *
17
+ * @DataProducer(
18
+ * id = "string_translations",
19
+ * name = @Translation("String translations"),
20
+ * description = @Translation("Returns the translations of a string."),
21
+ * produces = @ContextDefinition("any",
22
+ * label = @Translation("String translation"),
23
+ * multiple = TRUE
24
+ * ),
25
+ * consumes = {
26
+ * "sourceString" = @ContextDefinition("any",
27
+ * label = @Translation("Source string")
28
+ * )
29
+ * }
30
+ * )
31
+ */
32
+ class StringTranslations extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
33
+
34
+ /**
35
+ * The language manager service.
36
+ *
37
+ * @var \Drupal\Core\Language\LanguageManagerInterface
38
+ */
39
+ protected $languageManager;
40
+
41
+ /**
42
+ * The locale storage service.
43
+ *
44
+ * @var \Drupal\locale\StringStorageInterface
45
+ */
46
+ protected $localeStorage;
47
+
48
+ public function __construct(
49
+ array $configuration,
50
+ $plugin_id,
51
+ $plugin_definition,
52
+ LanguageManagerInterface $language_manager,
53
+ StringStorageInterface $locale_storage
54
+ ) {
55
+ parent::__construct($configuration, $plugin_id, $plugin_definition);
56
+ $this->languageManager = $language_manager;
57
+ $this->localeStorage = $locale_storage;
58
+ }
59
+
60
+ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
61
+ return new static(
62
+ $configuration,
63
+ $plugin_id,
64
+ $plugin_definition,
65
+ $container->get('language_manager'),
66
+ $container->get('locale.storage'),
67
+ );
68
+ }
69
+
70
+ /**
71
+ * Resolver.
72
+ *
73
+ * @param Drupal\locale\StringInterface $sourceString
74
+ * @return TranslationString[]
75
+ */
76
+ public function resolve(StringInterface $sourceString, FieldContext $context) {
77
+ $context->addCacheTags(['locale']);
78
+ $languages = $this->languageManager->getLanguages();
79
+ $translations = [];
80
+ foreach ($languages as $language) {
81
+ $translatedStrings = $this->localeStorage->getTranslations([
82
+ 'lid' => $sourceString->getId(),
83
+ 'language' => $language->getId(),
84
+ ]);
85
+ if (!empty($translatedStrings)) {
86
+ $translatedString = reset($translatedStrings);
87
+ if ($translatedString->isTranslation()) {
88
+ $translations[$language->getId()] = $translatedString;
89
+ }
90
+ }
91
+ }
92
+ return $translations;
93
+ }
94
+ }
@@ -0,0 +1,51 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\Directive;
4
+
5
+ use Drupal\Core\Plugin\PluginBase;
6
+ use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
7
+ use Drupal\graphql\GraphQL\ResolverBuilder;
8
+ use Drupal\graphql_directives\DirectiveInterface;
9
+ use Drupal\graphql_directives\Plugin\GraphQL\Directive\ArgumentTrait;
10
+
11
+ /**
12
+ * @Directive(
13
+ * id = "fetchEntity",
14
+ * description = "Fetch an entity or entity revision based on id, rid or route",
15
+ * arguments = {
16
+ * "type" = "String",
17
+ * "id" = "String",
18
+ * "rid" = "String",
19
+ * "language" = "String",
20
+ * "operation" = "String",
21
+ * "loadLatestRevision" = "Boolean"
22
+ * }
23
+ * )
24
+ */
25
+ class EntityFetch extends PluginBase implements DirectiveInterface {
26
+ use ArgumentTrait;
27
+
28
+ /**
29
+ * {@inheritDoc}
30
+ * @throws \Exception
31
+ */
32
+ public function buildResolver(ResolverBuilder $builder, array $arguments): ResolverInterface {
33
+ $resolver = $builder->produce('fetch_entity')
34
+ ->map('type', $this->argumentResolver($arguments['type'], $builder))
35
+ ->map('id', $this->argumentResolver($arguments['id'], $builder));
36
+
37
+ $argsMap = [
38
+ 'revision_id' => 'rid',
39
+ 'language' => 'language',
40
+ 'access_operation' => 'operation',
41
+ 'load_latest_revision' => 'loadLatestRevision',
42
+ ];
43
+ foreach($argsMap as $argParameter => $argField) {
44
+ if (isset($arguments[$argField])) {
45
+ $resolver->map($argParameter, $this->argumentResolver($arguments[$argField], $builder));
46
+ }
47
+ }
48
+ return $resolver;
49
+ }
50
+
51
+ }
@@ -0,0 +1,30 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\Directive;
4
+
5
+ use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
6
+ use Drupal\graphql\GraphQL\ResolverBuilder;
7
+ use Drupal\graphql_directives\DirectiveInterface;
8
+
9
+ /**
10
+ * @deprecated
11
+ *
12
+ * Duplicates EntityFeed::resolveTranslations().
13
+ *
14
+ * @Directive(
15
+ * id = "entityTranslationsWithDefault"
16
+ * )
17
+ */
18
+ class EntityTranslationsWithDefault implements DirectiveInterface {
19
+
20
+ public function buildResolver(ResolverBuilder $builder, $arguments): ResolverInterface {
21
+ return $builder->defaultValue(
22
+ $builder->compose(
23
+ $builder->produce('entity_translations')->map('entity', $builder->fromParent()),
24
+ $builder->callback(fn ($entities) => $entities ? array_filter($entities) : NULL)
25
+ ),
26
+ $builder->callback(fn ($value) => [$value])
27
+ );
28
+ }
29
+
30
+ }
@@ -0,0 +1,27 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\Directive;
4
+
5
+ use Drupal\Core\Plugin\PluginBase;
6
+ use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
7
+ use Drupal\graphql\GraphQL\ResolverBuilder;
8
+ use Drupal\graphql_directives\DirectiveInterface;
9
+
10
+ /**
11
+ * @Directive(
12
+ * id = "focalPoint",
13
+ * description = "Retrieve focal point coordinates for an image source.",
14
+ * arguments = {}
15
+ * )
16
+ */
17
+ class FocalPoint extends PluginBase implements DirectiveInterface {
18
+
19
+ /**
20
+ * {@inheritDoc}
21
+ * @throws \Exception
22
+ */
23
+ public function buildResolver(ResolverBuilder $builder, array $arguments): ResolverInterface {
24
+ return $builder->produce('focal_point')->map('image_props', $builder->fromParent());
25
+ }
26
+
27
+ }
@@ -0,0 +1,27 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\Directive;
4
+
5
+ use Drupal\Core\Plugin\PluginBase;
6
+ use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
7
+ use Drupal\graphql\GraphQL\ResolverBuilder;
8
+ use Drupal\graphql_directives\DirectiveInterface;
9
+
10
+ /**
11
+ * @Directive(
12
+ * id = "imageProps",
13
+ * description = "Retrieve the properties of an image.",
14
+ * arguments = {}
15
+ * )
16
+ */
17
+ class ImageProps extends PluginBase implements DirectiveInterface {
18
+
19
+ /**
20
+ * {@inheritDoc}
21
+ * @throws \Exception
22
+ */
23
+ public function buildResolver(ResolverBuilder $builder, array $arguments): ResolverInterface {
24
+ return $builder->produce('image_props')->map('entity', $builder->fromParent());
25
+ }
26
+
27
+ }
@@ -0,0 +1,27 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\Directive;
4
+
5
+ use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
6
+ use Drupal\graphql\GraphQL\ResolverBuilder;
7
+ use Drupal\graphql_directives\DirectiveInterface;
8
+ use Drupal\system\Entity\Menu;
9
+
10
+ /**
11
+ * @deprecated
12
+ *
13
+ * Duplicates MenuFeed::resolveLangcode().
14
+ *
15
+ * @Directive(
16
+ * id = "menuLangcode"
17
+ * )
18
+ */
19
+ class MenuLangcode implements DirectiveInterface {
20
+
21
+ public function buildResolver(ResolverBuilder $builder, $arguments): ResolverInterface {
22
+ return $builder->callback(
23
+ fn (Menu $value) => $value->language()->getId()
24
+ );
25
+ }
26
+
27
+ }
@@ -0,0 +1,36 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\Directive;
4
+
5
+ use Drupal\Core\Language\LanguageInterface;
6
+ use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
7
+ use Drupal\graphql\GraphQL\ResolverBuilder;
8
+ use Drupal\graphql_directives\DirectiveInterface;
9
+ use Drupal\system\Entity\Menu;
10
+
11
+ /**
12
+ *
13
+ * @deprecated
14
+ *
15
+ * Duplicates MenuFeed::resolveTranslations().
16
+ *
17
+ * @Directive(
18
+ * id = "menuTranslations"
19
+ * )
20
+ */
21
+ class MenuTranslations implements DirectiveInterface {
22
+
23
+ public function buildResolver(ResolverBuilder $builder, $arguments): ResolverInterface {
24
+ return $builder->compose($builder->callback(function(Menu $menu) {
25
+ return array_map(
26
+ function(LanguageInterface $lang) use ($menu) {
27
+ $clone = clone $menu;
28
+ $clone->set('langcode', $lang->getId());
29
+ return $clone;
30
+ },
31
+ \Drupal::languageManager()->getLanguages()
32
+ );
33
+ }));
34
+ }
35
+
36
+ }
@@ -0,0 +1,39 @@
1
+ <?php
2
+
3
+ namespace Drupal\silverback_gatsby\Plugin\GraphQL\Directive;
4
+
5
+ use Drupal\Core\Entity\ContentEntityInterface;
6
+ use Drupal\Core\Entity\EntityInterface;
7
+ use Drupal\graphql\GraphQL\Resolver\ResolverInterface;
8
+ use Drupal\graphql\GraphQL\ResolverBuilder;
9
+ use Drupal\graphql_directives\DirectiveInterface;
10
+
11
+ /**
12
+ * @deprecated
13
+ *
14
+ * Duplicates a part of SilverbackGatsbySchemaExtension::addFieldResolvers().
15
+ *
16
+ * @Directive(
17
+ * id = "silverbackGatsbyEntityId"
18
+ * )
19
+ */
20
+ class SilverbackGatsbyEntityId implements DirectiveInterface {
21
+
22
+ public function buildResolver(ResolverBuilder $builder, $arguments): ResolverInterface {
23
+ return $builder->callback(function(EntityInterface $entity) {
24
+ if ($entity instanceof ContentEntityInterface) {
25
+ $isTranslatable = FALSE;
26
+ if (\Drupal::hasService('content_translation.manager')) {
27
+ /** @var \Drupal\content_translation\ContentTranslationManagerInterface $ctm */
28
+ $ctm = \Drupal::service('content_translation.manager');
29
+ $isTranslatable = $ctm->isEnabled($entity->getEntityTypeId(), $entity->bundle());
30
+ }
31
+ return $isTranslatable
32
+ ? $entity->uuid() . ':' . $entity->language()->getId()
33
+ : $entity->uuid();
34
+ }
35
+ return $entity->id();
36
+ });
37
+ }
38
+
39
+ }