Chris@14: uuid = $uuid; Chris@14: $this->region = $region; Chris@14: $this->configuration = $configuration; Chris@14: $this->additional = $additional; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns the renderable array for this component. Chris@14: * Chris@14: * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts Chris@14: * An array of available contexts. Chris@14: * @param bool $in_preview Chris@14: * TRUE if the component is being previewed, FALSE otherwise. Chris@14: * Chris@14: * @return array Chris@14: * A renderable array representing the content of the component. Chris@14: */ Chris@14: public function toRenderArray(array $contexts = [], $in_preview = FALSE) { Chris@14: $event = new SectionComponentBuildRenderArrayEvent($this, $contexts, $in_preview); Chris@14: $this->eventDispatcher()->dispatch(LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY, $event); Chris@14: $output = $event->getBuild(); Chris@14: $event->getCacheableMetadata()->applyTo($output); Chris@14: return $output; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Gets any arbitrary property for the component. Chris@14: * Chris@14: * @param string $property Chris@14: * The property to retrieve. Chris@14: * Chris@14: * @return mixed Chris@14: * The value for that property, or NULL if the property does not exist. Chris@14: */ Chris@14: public function get($property) { Chris@14: if (property_exists($this, $property)) { Chris@14: $value = isset($this->{$property}) ? $this->{$property} : NULL; Chris@14: } Chris@14: else { Chris@14: $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL; Chris@14: } Chris@14: return $value; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Sets a value to an arbitrary property for the component. Chris@14: * Chris@14: * @param string $property Chris@14: * The property to use for the value. Chris@14: * @param mixed $value Chris@14: * The value to set. Chris@14: * Chris@14: * @return $this Chris@14: */ Chris@14: public function set($property, $value) { Chris@14: if (property_exists($this, $property)) { Chris@14: $this->{$property} = $value; Chris@14: } Chris@14: else { Chris@14: $this->additional[$property] = $value; Chris@14: } Chris@14: return $this; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Gets the region for the component. Chris@14: * Chris@14: * @return string Chris@14: * The region. Chris@14: */ Chris@14: public function getRegion() { Chris@14: return $this->region; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Sets the region for the component. Chris@14: * Chris@14: * @param string $region Chris@14: * The region. Chris@14: * Chris@14: * @return $this Chris@14: */ Chris@14: public function setRegion($region) { Chris@14: $this->region = $region; Chris@14: return $this; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Gets the weight of the component. Chris@14: * Chris@14: * @return int Chris@14: * The zero-based weight of the component. Chris@14: * Chris@14: * @throws \UnexpectedValueException Chris@14: * Thrown if the weight was never set. Chris@14: */ Chris@14: public function getWeight() { Chris@14: return $this->weight; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Sets the weight of the component. Chris@14: * Chris@14: * @param int $weight Chris@14: * The zero-based weight of the component. Chris@14: * Chris@14: * @return $this Chris@14: */ Chris@14: public function setWeight($weight) { Chris@14: $this->weight = $weight; Chris@14: return $this; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Gets the component plugin configuration. Chris@14: * Chris@14: * @return mixed[] Chris@14: * The component plugin configuration. Chris@14: */ Chris@14: protected function getConfiguration() { Chris@14: return $this->configuration; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Sets the plugin configuration. Chris@14: * Chris@14: * @param mixed[] $configuration Chris@14: * The plugin configuration. Chris@14: * Chris@14: * @return $this Chris@14: */ Chris@14: public function setConfiguration(array $configuration) { Chris@14: $this->configuration = $configuration; Chris@14: return $this; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Gets the plugin ID. Chris@14: * Chris@14: * @return string Chris@14: * The plugin ID. Chris@14: * Chris@14: * @throws \Drupal\Component\Plugin\Exception\PluginException Chris@14: * Thrown if the plugin ID cannot be found. Chris@14: */ Chris@14: public function getPluginId() { Chris@14: if (empty($this->configuration['id'])) { Chris@14: throw new PluginException(sprintf('No plugin ID specified for component with "%s" UUID', $this->uuid)); Chris@14: } Chris@14: return $this->configuration['id']; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Gets the UUID for this component. Chris@14: * Chris@14: * @return string Chris@14: * The UUID. Chris@14: */ Chris@14: public function getUuid() { Chris@14: return $this->uuid; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Gets the plugin for this component. Chris@14: * Chris@14: * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts Chris@14: * An array of contexts to set on the plugin. Chris@14: * Chris@14: * @return \Drupal\Component\Plugin\PluginInspectionInterface Chris@14: * The plugin. Chris@14: */ Chris@14: public function getPlugin(array $contexts = []) { Chris@14: $plugin = $this->pluginManager()->createInstance($this->getPluginId(), $this->getConfiguration()); Chris@14: if ($contexts && $plugin instanceof ContextAwarePluginInterface) { Chris@14: $this->contextHandler()->applyContextMapping($plugin, $contexts); Chris@14: } Chris@14: return $plugin; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Wraps the component plugin manager. Chris@14: * Chris@14: * @return \Drupal\Core\Block\BlockManagerInterface Chris@14: * The plugin manager. Chris@14: */ Chris@14: protected function pluginManager() { Chris@14: // @todo Figure out the best way to unify fields and blocks and components Chris@14: // in https://www.drupal.org/node/1875974. Chris@14: return \Drupal::service('plugin.manager.block'); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Wraps the context handler. Chris@14: * Chris@14: * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface Chris@14: * The context handler. Chris@14: */ Chris@14: protected function contextHandler() { Chris@14: return \Drupal::service('context.handler'); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Wraps the event dispatcher. Chris@14: * Chris@14: * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface Chris@14: * The event dispatcher. Chris@14: */ Chris@14: protected function eventDispatcher() { Chris@14: return \Drupal::service('event_dispatcher'); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Returns an array representation of the section component. Chris@14: * Chris@16: * Only use this method if you are implementing custom storage for sections. Chris@14: * Chris@14: * @return array Chris@14: * An array representation of the section component. Chris@14: */ Chris@14: public function toArray() { Chris@14: return [ Chris@14: 'uuid' => $this->getUuid(), Chris@14: 'region' => $this->getRegion(), Chris@14: 'configuration' => $this->getConfiguration(), Chris@14: 'additional' => $this->additional, Chris@14: 'weight' => $this->getWeight(), Chris@14: ]; Chris@14: } Chris@14: Chris@16: /** Chris@16: * Creates an object from an array representation of the section component. Chris@16: * Chris@16: * Only use this method if you are implementing custom storage for sections. Chris@16: * Chris@16: * @param array $component Chris@16: * An array of section component data in the format returned by ::toArray(). Chris@16: * Chris@16: * @return static Chris@16: * The section component object. Chris@16: */ Chris@16: public static function fromArray(array $component) { Chris@16: return (new static( Chris@16: $component['uuid'], Chris@16: $component['region'], Chris@16: $component['configuration'], Chris@16: $component['additional'] Chris@16: ))->setWeight($component['weight']); Chris@16: } Chris@16: Chris@14: }