You are here

class GridStackLayout in GridStack 8.2

Provides a GridStack class for Layout plugins.

Hierarchy

Expanded class hierarchy of GridStackLayout

File

src/Plugin/Layout/GridStackLayout.php, line 12

Namespace

Drupal\gridstack\Plugin\Layout
View source
class GridStackLayout extends GridStackLayoutBase {

  /**
   * {@inheritdoc}
   */
  public function build(array $regions) {
    ksort($regions);
    $settings = $this
      ->getConfiguration();
    $definition = $this
      ->getPluginDefinition();
    $name = $definition
      ->get('optionset');
    $optionset = GridStack::loadWithFallback($name);
    $variant = $this->manager
      ->getEngine($settings, 'variant')
      ->override($optionset, $settings);

    // Provides settings.
    $settings['optionset'] = $name;
    $settings['_stylizer'] = TRUE;
    $settings = array_filter($settings);
    $build = [
      'items' => $this
        ->interpolateItems($optionset, $settings, $regions),
      'optionset' => $optionset,
      'settings' => $settings,
      'layout' => $definition,
      'variant' => $variant,
    ];

    // Still a check in case it is removed by another alter.
    if ($library = $this->pluginDefinition
      ->getLibrary()) {
      $build['attached']['library'][] = $library;
    }

    // Must pass the layout blueprint to Layout Builder regardless empty.
    return $this->manager
      ->build($build);
  }

  /**
   * Interpolate data from Layout Builder to match original construct.
   *
   * We do this because we don't manually put regions into templates.
   * Instead structured with nested grids, if any, to have one template for any
   * known layout possibility: one or two dimensional layouts.
   * With everything being cached at D8, this shouldn't make much different than
   * hard-coded layouts at YML files which still need parsing anyway.
   */
  protected function interpolateItems($optionset, array &$settings, array $regions = []) {
    $id = 'box';
    $items = [];
    $config = isset($settings['regions']) ? $settings['regions'] : [];
    unset($settings['regions']);
    foreach (array_keys($optionset
      ->getLastBreakpoint()) as $delta) {
      $rid = GridStackDefault::regionId($delta);
      $box = [];

      // Remove top level settings to avoid leaking due to similarity.
      $box['settings'] = array_diff_key($settings, GridStackDefault::regionSettings());
      $box['settings'] = isset($config[$rid]) ? array_merge($box['settings'], $config[$rid]) : $box['settings'];
      if ($grids = $optionset
        ->getNestedGridsByDelta($delta)) {
        foreach (array_keys($grids) as $nid) {
          $rid = GridStackDefault::regionId($delta . '_' . $nid);

          // @todo recheck $box['settings'] = array_diff_key($box['settings'], GridStackDefault::regionSettings());
          // Preserves indices even if empty so to layout for Layout Builder.
          $box[$id][$nid][$id] = isset($regions[$rid]) && !Element::isEmpty($regions[$rid]) ? $regions[$rid] : [];
          $box[$id][$nid]['settings'] = isset($config[$rid]) ? array_merge($box['settings'], $config[$rid]) : $box['settings'];
        }
      }
      else {

        // Preserves indices even if empty so to layout for Layout Builder.
        $box[$id] = isset($regions[$rid]) && !Element::isEmpty($regions[$rid]) ? $regions[$rid] : [];
      }
      $items[] = $box;
      unset($box);
    }
    return $items;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GridStackLayout::build public function Build a render array for layout with regions. Overrides LayoutDefault::build
GridStackLayout::interpolateItems protected function Interpolate data from Layout Builder to match original construct.
GridStackLayoutBase::$admin protected property The gridstack admin service.
GridStackLayoutBase::$currentUser protected property The current user.
GridStackLayoutBase::$fieldName protected property The field name to store media.
GridStackLayoutBase::$manager protected property The gridstack manager service.
GridStackLayoutBase::$styleForm protected property The gridstack stylizer form plugin.
GridStackLayoutBase::$stylizer protected property The gridstack stylizer service.
GridStackLayoutBase::buildConfigurationForm public function Form constructor. Overrides LayoutDefault::buildConfigurationForm
GridStackLayoutBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
GridStackLayoutBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides LayoutDefault::defaultConfiguration
GridStackLayoutBase::massageValues protected function Massage form values.
GridStackLayoutBase::submitConfigurationForm public function Form submission handler. Overrides LayoutDefault::submitConfigurationForm
GridStackLayoutBase::validateConfigurationForm public function Form validation handler. Overrides LayoutDefault::validateConfigurationForm
LayoutDefault::$pluginDefinition protected property The layout definition. Overrides PluginBase::$pluginDefinition
LayoutDefault::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 2
LayoutDefault::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
LayoutDefault::getPluginDefinition public function Overrides PluginBase::getPluginDefinition
LayoutDefault::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
LayoutDefault::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.