|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Blocks; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\block\Entity\Block; |
|
6
|
|
|
use Drupal\block_content\Plugin\Block\BlockContentBlock; |
|
7
|
|
|
use Drupal\Core\Condition\ConditionInterface; |
|
8
|
|
|
use Drupal\Core\Entity\EntityRepositoryInterface; |
|
9
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
10
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
11
|
|
|
use Drupal\Core\Theme\ThemeManagerInterface; |
|
12
|
|
|
use Drupal\Core\Url; |
|
13
|
|
|
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer; |
|
14
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
16
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* List all blocks within a theme region. |
|
20
|
|
|
* |
|
21
|
|
|
* TODO: Move this to `InternalUrl` (breaking change). |
|
22
|
|
|
* |
|
23
|
|
|
* @GraphQLField( |
|
24
|
|
|
* id = "blocks_by_region", |
|
25
|
|
|
* secure = true, |
|
26
|
|
|
* name = "blocksByRegion", |
|
27
|
|
|
* type = "Entity", |
|
28
|
|
|
* parents = {"Url", "Root"}, |
|
29
|
|
|
* multi = true, |
|
30
|
|
|
* arguments = { |
|
31
|
|
|
* "region" = "String" |
|
32
|
|
|
* } |
|
33
|
|
|
* ) |
|
34
|
|
|
*/ |
|
35
|
|
|
class BlocksByRegion extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The theme manager. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \Drupal\Core\Theme\ThemeManagerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $themeManager; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The entity type manager. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $entityTypeManager; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The entity repository. |
|
53
|
|
|
* |
|
54
|
|
|
* @var \Drupal\Core\Entity\EntityRepositoryInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $entityRepository; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The subrequest buffer service. |
|
60
|
|
|
* |
|
61
|
|
|
* @var \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $subRequestBuffer; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function create( |
|
69
|
|
|
ContainerInterface $container, |
|
70
|
|
|
array $configuration, |
|
71
|
|
|
$pluginId, |
|
72
|
|
|
$pluginDefinition |
|
73
|
|
|
) { |
|
74
|
|
|
return new static( |
|
75
|
|
|
$configuration, |
|
76
|
|
|
$pluginId, |
|
77
|
|
|
$pluginDefinition, |
|
78
|
|
|
$container->get('graphql.buffer.subrequest'), |
|
79
|
|
|
$container->get('theme.manager'), |
|
80
|
|
|
$container->get('entity_type.manager'), |
|
81
|
|
|
$container->get('entity.repository') |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __construct( |
|
89
|
|
|
array $configuration, |
|
90
|
|
|
$pluginId, |
|
91
|
|
|
$pluginDefinition, |
|
92
|
|
|
SubRequestBuffer $subRequestBuffer, |
|
93
|
|
|
ThemeManagerInterface $themeManager, |
|
94
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
|
95
|
|
|
EntityRepositoryInterface $entityRepository |
|
96
|
|
|
) { |
|
97
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
|
98
|
|
|
$this->subRequestBuffer = $subRequestBuffer; |
|
99
|
|
|
$this->themeManager = $themeManager; |
|
100
|
|
|
$this->entityTypeManager = $entityTypeManager; |
|
101
|
|
|
$this->entityRepository = $entityRepository; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
|
108
|
|
|
if ($value instanceof Url) { |
|
|
|
|
|
|
109
|
|
|
$activeTheme = $this->themeManager->getActiveTheme(); |
|
110
|
|
|
$blockStorage = $this->entityTypeManager->getStorage('block'); |
|
111
|
|
|
$blocks = $blockStorage->loadByProperties([ |
|
112
|
|
|
'theme' => $activeTheme->getName(), |
|
113
|
|
|
'region' => $args['region'], |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
$resolve = $this->subRequestBuffer->add($value, function () use ($blocks) { |
|
117
|
|
|
$blocks = array_filter($blocks, function (Block $block) { |
|
|
|
|
|
|
118
|
|
|
return array_reduce(iterator_to_array($block->getVisibilityConditions()), function($value, ConditionInterface $condition) { |
|
119
|
|
|
return $value && (!$condition->isNegated() == $condition->evaluate()); |
|
120
|
|
|
}, TRUE); |
|
121
|
|
|
}); |
|
122
|
|
|
|
|
123
|
|
|
uasort($blocks, '\Drupal\Block\Entity\Block::sort'); |
|
124
|
|
|
|
|
125
|
|
|
return $blocks; |
|
126
|
|
|
}); |
|
127
|
|
|
|
|
128
|
|
|
return function ($value, array $args, ResolveInfo $info) use ($resolve) { |
|
|
|
|
|
|
129
|
|
|
$blocks = array_map(function(Block $block) { |
|
130
|
|
|
$plugin = $block->getPlugin(); |
|
131
|
|
|
if ($plugin instanceof BlockContentBlock) { |
|
|
|
|
|
|
132
|
|
|
return $this->entityRepository->loadEntityByUuid('block_content', $plugin->getDerivativeId()); |
|
133
|
|
|
} |
|
134
|
|
|
else { |
|
135
|
|
|
return $block; |
|
136
|
|
|
} |
|
137
|
|
|
}, $resolve()); |
|
138
|
|
|
|
|
139
|
|
|
foreach ($blocks as $block) { |
|
140
|
|
|
yield $block; |
|
141
|
|
|
} |
|
142
|
|
|
}; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|