You are here

abstract class GridStackLayoutBase in GridStack 8.2

Provides a GridStack base class for Layout plugins.

Hierarchy

Expanded class hierarchy of GridStackLayoutBase

File

src/Plugin/Layout/GridStackLayoutBase.php, line 19

Namespace

Drupal\gridstack\Plugin\Layout
View source
abstract class GridStackLayoutBase extends LayoutDefault implements ContainerFactoryPluginInterface, PluginFormInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The gridstack manager service.
   *
   * @var \Drupal\gridstack\GridStackManagerInterface
   */
  protected $manager;

  /**
   * The gridstack stylizer service.
   *
   * @var \Drupal\gridstack\GridStackStylizerInterface
   */
  protected $stylizer;

  /**
   * The gridstack stylizer form plugin.
   *
   * @var \Drupal\gridstack\GridStackStylizerPluginInterface
   */
  protected $styleForm;

  /**
   * The gridstack admin service.
   *
   * @var \Drupal\gridstack\Form\GridStackAdminInterface
   */
  protected $admin;

  /**
   * The field name to store media.
   *
   * @var string
   */
  protected $fieldName;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    $instance->currentUser = $container
      ->get('current_user');
    $instance->manager = $container
      ->get('gridstack.manager');
    $instance->stylizer = $instance->manager
      ->stylizer();
    $config = [];
    foreach ([
      'field_name',
      'optionset',
    ] as $key) {
      if (isset($configuration[$key])) {
        $config[$key] = $configuration[$key];
      }
    }
    $instance->styleForm = $instance->stylizer
      ->form($config);
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'regions' => [],
    ] + GridStackDefault::layoutSettings() + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    if ($settings = $form_state
      ->getValue('settings')) {
      $this->styleForm
        ->cleanupStyles($settings['styles']);
      if (isset($settings['global'])) {
        foreach ($settings['global'] as $key => $value) {
          $settings[$key] = $value;
        }
        unset($settings['global']);
      }
      unset($settings['current_selection'], $settings['preset_classes'], $settings['preset_row_classes']);
      foreach ($settings as $key => &$value) {
        $this
          ->massageValues($value, $key);
        $this->configuration[$key] = $value;
      }
    }
    unset($this->configuration['global'], $this->configuration['settings']);
    $regions = [];
    if ($values = $form_state
      ->getValue('regions')) {
      foreach ($values as $name => &$region) {
        $this->styleForm
          ->cleanupStyles($region['styles']);
        foreach ($region as $key => &$value) {
          $this
            ->massageValues($value, $key);
          if (in_array($key, [
            'preset_classes',
            'preset_row_classes',
          ])) {
            continue;
          }
          $regions[$name][$key] = $value;
        }
      }
    }
    $this->configuration['regions'] = $regions;
  }

  /**
   * Massage form values.
   */
  protected function massageValues(&$value, $key) {
    if ($key == 'styles') {
      foreach ([
        'alpha',
        'opacity',
      ] as $k) {
        if (isset($value[$k]) && $value[$k] == '1') {
          $value[$k] = '';
        }
      }
    }
    $value = is_string($value) ? trim($value) : $value;
    $value = is_array($value) ? array_filter($value) : $value;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::validateConfigurationForm($form, $form_state);
    $this->styleForm
      ->validateConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

    // This form may be loaded as a subform Layout Builder, etc.
    // More info: #2536646, #2798261, #2774077.
    $form_state2 = $form_state instanceof SubformStateInterface ? $form_state
      ->getCompleteFormState() : $form_state;
    $form = parent::buildConfigurationForm($form, $form_state2);
    $access_ui = $this->currentUser
      ->hasPermission('administer gridstack');
    $config = $this
      ->getConfiguration();
    $definition = $this
      ->getPluginDefinition();
    $name = $definition
      ->get('optionset');
    $optionset = GridStack::loadWithFallback($name);
    $settings = [];
    $entity_form = $form_state2
      ->getFormObject();
    $extras = $this->styleForm
      ->getEntityData($entity_form);
    if (isset($form['label'])) {
      $name_nice = Unicode::ucfirst($name);
      $form['label']['#attributes']['placeholder'] = $this
        ->t('@name', [
        '@name' => $name_nice,
      ]);
      $form['label']['#wrapper_attributes']['class'][] = 'is-gs-aside';
      $form['label']['#description'] = $this
        ->t('A region has direct contents. A container contains multiple regions.');
      $default = empty($config['label']) ? str_replace('_', ' ', $name_nice) : $config['label'];
      $form['label']['#default_value'] = $form_state2
        ->getValue('label', $default);
    }
    foreach (GridStackDefault::layoutSettings() as $key => $value) {
      $default = isset($config[$key]) ? $config[$key] : $value;
      $settings[$key] = $form_state2
        ->getValue([
        'settings',
        $key,
      ], $default);
    }
    $this->manager
      ->getEngine($settings, 'variant')
      ->override($optionset, $settings);

    // Allows regions being modified by variants.
    $regions = $optionset
      ->prepareRegions(FALSE);
    $settings['_fullwidth'] = TRUE;
    $settings['_container'] = TRUE;
    $settings['_scope'] = GridStackDefault::ROOT;
    $settings['_delta'] = 0;
    $this->fieldName = $settings['field_name'];
    $field_options = $extras ? $this->styleForm
      ->getLayoutFieldOptions() : [];
    $settings['access_media'] = $access_media = $this->currentUser
      ->hasPermission('access media overview');
    if (empty($settings['field_name']) && $field_options) {
      $this->fieldName = $settings['field_name'] = reset($field_options);
    }
    $extras['field_options'] = $field_options;
    $style_config = [
      'field_name' => $this->fieldName,
      'optionset' => $name,
    ];
    $this->styleForm
      ->setConfiguration($style_config);
    $this->styleForm
      ->setFieldName($this->fieldName);
    $description = $this
      ->t('Options require saving the form first.');
    if ($this->manager
      ->getModuleHandler()
      ->moduleExists('gridstack_ui') && $access_ui) {
      $description .= '<br>' . $this
        ->t('[<a href=":url" class="is-gs-edit-link">Edit @id</a>]', [
        ':url' => $optionset
          ->toUrl('edit-form')
          ->toString(),
        '@id' => Xss::filter($optionset
          ->label()),
      ]);
    }
    $form['settings'] = [
      '#type' => 'details',
      '#tree' => TRUE,
      '#open' => TRUE,
      '#weight' => 30,
      '#title' => $this
        ->t('Global settings'),
      '#description' => $description,
      '#attributes' => [
        'class' => [
          'form-wrapper--gs',
          'is-gs-main-settings',
        ],
        'data-gs-region' => $settings['_scope'],
      ],
      '#field_name' => $this->fieldName,
      '#parents' => [
        'layout_settings',
        'settings',
      ],
    ];
    $form['settings'] = array_merge($form['settings'], $this->styleForm
      ->buildConfigurationForm($optionset, $form_state2, $settings, $extras));
    $form['regions'] = [
      '#type' => 'container',
      '#tree' => TRUE,
      '#parents' => [
        'layout_settings',
        'regions',
      ],
    ];

    // Reset settings. The delta is faked for Media Library where 0 is
    // reserved by the non-region, top-level settings. Also to support the
    // styling of non-region, aka. container. The rid is faked, too.
    $delta = 1;
    $rid = 0;
    $settings2 = [];
    foreach ($regions as $region => $info) {
      foreach (GridStackDefault::regionSettings() as $key => $value) {
        $default = isset($config['regions'][$region][$key]) ? $config['regions'][$region][$key] : $value;
        $default = $form_state2
          ->getValue([
          'regions',
          $region,
          $key,
        ], $default);
        $settings2['regions'][$region][$key] = $default;
      }
      $form['regions'][$region] = [
        '#type' => 'details',
        '#title' => $this
          ->t('@type: <em>@label</em>', [
          '@type' => $info['type'],
          '@label' => $info['label'],
        ]),
        '#open' => FALSE,
        '#tree' => TRUE,
        '#attributes' => [
          'data-gs-region' => $region,
          'class' => [
            'form-wrapper--gs',
          ],
        ],
        '#field_name' => $this->fieldName,
        '#parents' => [
          'layout_settings',
          'regions',
          $region,
        ],
      ];
      if (isset($info['cid'])) {
        $form['regions'][$region]['#attributes']['data-gs-region-container'] = $info['cid'];
      }
      $settings2['regions'][$region]['field_name'] = $this->fieldName;
      $settings2['regions'][$region]['access_media'] = $access_media;
      $settings2['regions'][$region]['_fullwidth'] = !empty($info['_fw']);
      $settings2['regions'][$region]['_rid'] = $rid;
      $settings2['regions'][$region]['_scope'] = $region;
      $settings2['regions'][$region]['_delta'] = $delta;
      $settings2['regions'][$region]['_container'] = $info['type'] == GridStackDefault::CONTAINER;
      $form['regions'][$region] = array_merge($form['regions'][$region], $this->styleForm
        ->buildConfigurationForm($optionset, $form_state2, $settings2['regions'][$region], $extras));
      if ($info['type'] == 'Region') {
        $rid++;
      }
      $delta++;
    }

    // Provides color palettes and assets.
    $this->styleForm
      ->closingForm($form, $settings);
    return $form;
  }

}

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
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::build public function Build a render array for layout with regions. Overrides LayoutInterface::build 3
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.