3 namespace Drupal\layout_builder;
5 use Drupal\Component\Plugin\Exception\PluginException;
6 use Drupal\Core\Plugin\ContextAwarePluginInterface;
7 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
10 * Provides a value object for a section component.
12 * A component represents the smallest part of a layout (for example, a block).
13 * Components wrap a renderable plugin, currently using
14 * \Drupal\Core\Block\BlockPluginInterface, and contain the layout region
15 * within the section layout where the component will be rendered.
18 * Layout Builder is currently experimental and should only be leveraged by
19 * experimental modules and development releases of contributed modules.
20 * See https://www.drupal.org/core/experimental for more information.
22 * @see \Drupal\Core\Layout\LayoutDefinition
23 * @see \Drupal\layout_builder\Section
24 * @see \Drupal\layout_builder\SectionStorageInterface
26 * @todo Determine whether to retain the name 'component' in
27 * https://www.drupal.org/project/drupal/issues/2929783.
28 * @todo Determine whether an interface will be provided for this in
29 * https://www.drupal.org/project/drupal/issues/2930334.
31 class SectionComponent {
34 * The UUID of the component.
41 * The region the component is placed in.
48 * An array of plugin configuration.
52 protected $configuration;
55 * The weight of the component.
59 protected $weight = 0;
62 * Any additional properties and values.
66 protected $additional = [];
69 * Constructs a new SectionComponent.
73 * @param string $region
75 * @param mixed[] $configuration
76 * The plugin configuration.
77 * @param mixed[] $additional
78 * An additional values.
80 public function __construct($uuid, $region, array $configuration = [], array $additional = []) {
82 $this->region = $region;
83 $this->configuration = $configuration;
84 $this->additional = $additional;
88 * Returns the renderable array for this component.
90 * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
91 * An array of available contexts.
92 * @param bool $in_preview
93 * TRUE if the component is being previewed, FALSE otherwise.
96 * A renderable array representing the content of the component.
98 public function toRenderArray(array $contexts = [], $in_preview = FALSE) {
99 $event = new SectionComponentBuildRenderArrayEvent($this, $contexts, $in_preview);
100 $this->eventDispatcher()->dispatch(LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY, $event);
101 $output = $event->getBuild();
102 $event->getCacheableMetadata()->applyTo($output);
107 * Gets any arbitrary property for the component.
109 * @param string $property
110 * The property to retrieve.
113 * The value for that property, or NULL if the property does not exist.
115 public function get($property) {
116 if (property_exists($this, $property)) {
117 $value = isset($this->{$property}) ? $this->{$property} : NULL;
120 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
126 * Sets a value to an arbitrary property for the component.
128 * @param string $property
129 * The property to use for the value.
130 * @param mixed $value
135 public function set($property, $value) {
136 if (property_exists($this, $property)) {
137 $this->{$property} = $value;
140 $this->additional[$property] = $value;
146 * Gets the region for the component.
151 public function getRegion() {
152 return $this->region;
156 * Sets the region for the component.
158 * @param string $region
163 public function setRegion($region) {
164 $this->region = $region;
169 * Gets the weight of the component.
172 * The zero-based weight of the component.
174 * @throws \UnexpectedValueException
175 * Thrown if the weight was never set.
177 public function getWeight() {
178 return $this->weight;
182 * Sets the weight of the component.
185 * The zero-based weight of the component.
189 public function setWeight($weight) {
190 $this->weight = $weight;
195 * Gets the component plugin configuration.
198 * The component plugin configuration.
200 protected function getConfiguration() {
201 return $this->configuration;
205 * Sets the plugin configuration.
207 * @param mixed[] $configuration
208 * The plugin configuration.
212 public function setConfiguration(array $configuration) {
213 $this->configuration = $configuration;
218 * Gets the plugin ID.
223 * @throws \Drupal\Component\Plugin\Exception\PluginException
224 * Thrown if the plugin ID cannot be found.
226 public function getPluginId() {
227 if (empty($this->configuration['id'])) {
228 throw new PluginException(sprintf('No plugin ID specified for component with "%s" UUID', $this->uuid));
230 return $this->configuration['id'];
234 * Gets the UUID for this component.
239 public function getUuid() {
244 * Gets the plugin for this component.
246 * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
247 * An array of contexts to set on the plugin.
249 * @return \Drupal\Component\Plugin\PluginInspectionInterface
252 public function getPlugin(array $contexts = []) {
253 $plugin = $this->pluginManager()->createInstance($this->getPluginId(), $this->getConfiguration());
254 if ($contexts && $plugin instanceof ContextAwarePluginInterface) {
255 $this->contextHandler()->applyContextMapping($plugin, $contexts);
261 * Wraps the component plugin manager.
263 * @return \Drupal\Core\Block\BlockManagerInterface
264 * The plugin manager.
266 protected function pluginManager() {
267 // @todo Figure out the best way to unify fields and blocks and components
268 // in https://www.drupal.org/node/1875974.
269 return \Drupal::service('plugin.manager.block');
273 * Wraps the context handler.
275 * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface
276 * The context handler.
278 protected function contextHandler() {
279 return \Drupal::service('context.handler');
283 * Wraps the event dispatcher.
285 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
286 * The event dispatcher.
288 protected function eventDispatcher() {
289 return \Drupal::service('event_dispatcher');
293 * Returns an array representation of the section component.
296 * This is intended for use by a storage mechanism for section components.
299 * An array representation of the section component.
301 public function toArray() {
303 'uuid' => $this->getUuid(),
304 'region' => $this->getRegion(),
305 'configuration' => $this->getConfiguration(),
306 'additional' => $this->additional,
307 'weight' => $this->getWeight(),