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