You are here

trait GridStackLayoutTrait in GridStack 8

A Trait common for optional layout integration.

@todo: Remove, and merge back into GridStackLayout for Drupal 8.x-3.

Hierarchy

File

src/Layout/GridStackLayoutTrait.php, line 13

Namespace

Drupal\gridstack\Layout
View source
trait GridStackLayoutTrait {

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

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

  /**
   * Returns regions based on available grids.
   *
   * @param mixed $optionset
   *   The \Drupal\gridstack\Entity\GridStack optionset instance.
   * @param bool $clean
   *   The flag to exclude region containers.
   *
   * @return array
   *   Available regions by the given $optionset parameter, else empty.
   */
  public static function prepareRegions($optionset = NULL, $clean = TRUE) {
    $grids = $optionset
      ->getEndBreakpointGrids();
    $regions = [];
    foreach ($grids as $delta => $grid) {
      $label_index = $delta + 1;
      $label = 'GridStack  ' . $label_index;
      $regions['gridstack_' . $delta]['label'] = new TranslatableMarkup('@label', [
        '@label' => $label,
      ]);

      // With nested grids, its container doesn't contain contents, but grids.
      $nested_grids = $optionset
        ->getNestedGridsByDelta($delta);
      $is_nested = array_filter($nested_grids);
      if (!empty($is_nested)) {

        // Remove container since the actual contents are moved, if required.
        if ($clean) {
          unset($regions['gridstack_' . $delta]);
        }
        foreach ($nested_grids as $nested_delta => $nested_grid) {
          $label_index_nested = $nested_delta + 1;
          $label = 'GridStack  ' . $label_index . ':' . $label_index_nested;
          $regions['gridstack_' . $delta . '_' . $nested_delta]['label'] = new TranslatableMarkup('@label', [
            '@label' => $label,
          ]);
        }
      }
    }
    return $regions;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'regions' => [],
      'attributes' => '',
      'skin' => '',
      'wrapper' => 'div',
      'wrapper_classes' => '',
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildFormElements(array $settings = []) {
    $wrapper_options = [
      'div' => 'Div',
      'article' => 'Article',
      'aside' => 'Aside',
      'figure' => 'Figure',
      'header' => 'Header',
      'main' => 'Main',
      'footer' => 'Footer',
      'section' => 'Section',
    ];
    $elements['wrapper'] = [
      '#type' => 'select',
      '#options' => $wrapper_options,
      '#title' => $this
        ->t('Wrapper'),
      '#default_value' => isset($settings['wrapper']) ? $settings['wrapper'] : 'div',
    ];
    $elements['wrapper_classes'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Classes'),
      '#description' => $this
        ->t('E.g.: text-left text-lowercase no-gutters'),
      '#default_value' => isset($settings['wrapper_classes']) ? $settings['wrapper_classes'] : '',
    ];
    $elements['attributes'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Attributes'),
      '#description' => $this
        ->t('E.g.: role|navigation,data-something|some value'),
      '#default_value' => isset($settings['attributes']) ? strip_tags($settings['attributes']) : '',
      '#weight' => 20,
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $regions = $form_state
      ->getValue('regions');
    $settings = $form_state
      ->getValue('settings');
    if (!empty($settings)) {
      foreach ($settings as $key => $value) {
        $this->configuration[$key] = trim(strip_tags($value));
      }
      unset($this->configuration['settings']);
    }
    if (!empty($regions)) {
      $stored_regions = [];
      foreach ($regions as $name => $info) {
        $region = $form_state
          ->getValue([
          'regions',
          $name,
        ]);
        foreach ($region as $key => $value) {
          $stored_regions[$name][$key] = trim(strip_tags($value));
          if (empty($value)) {
            unset($stored_regions[$name][$key]);
          }
        }
      }
      $this->configuration['regions'] = $stored_regions;
    }
  }

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

    // Satisfy \Drupal\Core\Plugin\PluginFormInterface.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GridStackLayoutTrait::$currentUser protected property The current user.
GridStackLayoutTrait::$manager protected property The gridstack manager service.
GridStackLayoutTrait::buildFormElements public function
GridStackLayoutTrait::defaultConfiguration public function
GridStackLayoutTrait::prepareRegions public static function Returns regions based on available grids.
GridStackLayoutTrait::submitConfigurationForm public function
GridStackLayoutTrait::validateConfigurationForm public function