Unverified Commit 5e7fb734 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3115503 by clayfreeman, tim.plunkett, alexpott, andypost: Support...

Issue #3115503 by clayfreeman, tim.plunkett, alexpott, andypost: Support context aware layout plugins
parent a4095b9b
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -362,6 +362,11 @@ layout_plugin.settings:
    label:
      type: label
      label: 'Label'
    context_mapping:
      type: sequence
      label: 'Context assignments'
      sequence:
        type: string

layout_plugin.settings.*:
  type: layout_plugin.settings
+7 −0
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginFormInterface;

@@ -12,6 +14,9 @@
 */
class LayoutDefault extends PluginBase implements LayoutInterface, PluginFormInterface {

  use ContextAwarePluginAssignmentTrait;
  use ContextAwarePluginTrait;

  /**
   * The layout definition.
   *
@@ -95,6 +100,8 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
      '#title' => $this->t('Administrative label'),
      '#default_value' => $this->configuration['label'],
    ];
    $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
    $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
    return $form;
  }

+14 −1
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\Core\Layout;

use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionTrait;
use Drupal\Component\Plugin\Definition\DerivablePluginDefinitionInterface;
use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
use Drupal\Component\Plugin\Definition\PluginDefinition;
@@ -11,8 +13,9 @@
/**
 * Provides an implementation of a layout definition and its metadata.
 */
class LayoutDefinition extends PluginDefinition implements PluginDefinitionInterface, DerivablePluginDefinitionInterface, DependentPluginDefinitionInterface {
class LayoutDefinition extends PluginDefinition implements PluginDefinitionInterface, DerivablePluginDefinitionInterface, DependentPluginDefinitionInterface, ContextAwarePluginDefinitionInterface {

  use ContextAwarePluginDefinitionTrait;
  use DependentPluginDefinitionTrait;

  /**
@@ -129,6 +132,16 @@ class LayoutDefinition extends PluginDefinition implements PluginDefinitionInter
   *   An array of values from the annotation.
   */
  public function __construct(array $definition) {
    // If there are context definitions in the plugin definition, they should
    // be added to this object using ::addContextDefinition() so that they can
    // be manipulated using other ContextAwarePluginDefinitionInterface methods.
    if (isset($definition['context_definitions'])) {
      foreach ($definition['context_definitions'] as $name => $context_definition) {
        $this->addContextDefinition($name, $context_definition);
      }
      unset($definition['context_definitions']);
    }

    foreach ($definition as $property => $value) {
      $this->set($property, $value);
    }
+2 −1
Original line number Diff line number Diff line
@@ -6,11 +6,12 @@
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;

/**
 * Provides an interface for static Layout plugins.
 */
interface LayoutInterface extends PluginInspectionInterface, DerivativeInspectionInterface, ConfigurableInterface, DependentPluginInterface {
interface LayoutInterface extends PluginInspectionInterface, DerivativeInspectionInterface, ConfigurableInterface, DependentPluginInterface, ContextAwarePluginInterface {

  /**
   * Build a render array for layout with regions.
+33 −0
Original line number Diff line number Diff line
@@ -5,6 +5,11 @@
 * Post update functions for Layout Builder.
 */

use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;

use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;

/**
 * Implements hook_removed_post_updates().
 */
@@ -34,3 +39,31 @@ function layout_builder_removed_post_updates() {
function layout_builder_post_update_override_entity_form_controller() {
  // Empty post-update hook.
}

/**
 * Update view displays that use Layout Builder to add empty context mappings.
 */
function layout_builder_post_update_section_storage_context_mapping(&$sandbox = []) {
  $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);

  $callback = function (EntityViewDisplayInterface $display) {
    $needs_update = FALSE;

    // Only update entity view displays where Layout Builder is enabled.
    if ($display instanceof LayoutEntityDisplayInterface && $display->isLayoutBuilderEnabled()) {
      foreach ($display->getSections() as $section) {
        // Add an empty context mapping to each section where one doesn't exist.
        $section->setLayoutSettings($section->getLayoutSettings() + [
          'context_mapping' => [],
        ]);

        // Flag this display as needing to be updated.
        $needs_update = TRUE;
      }
    }

    return $needs_update;
  };

  $config_entity_updater->update($sandbox, 'entity_view_display', $callback);
}
Loading