You are here

class GridStackViews in GridStack 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/style/GridStackViews.php \Drupal\gridstack\Plugin\views\style\GridStackViews

GridStack style plugin.

Hierarchy

  • class \Drupal\gridstack\Plugin\views\style\GridStackViews extends \Drupal\blazy\Dejavu\BlazyStylePluginBase

Expanded class hierarchy of GridStackViews

File

src/Plugin/views/style/GridStackViews.php, line 17

Namespace

Drupal\gridstack\Plugin\views\style
View source
class GridStackViews extends BlazyStylePluginBase {

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

  /**
   * Constructs a GridStackManager object.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, BlazyManagerInterface $blazy_manager, GridStackManagerInterface $manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $blazy_manager);
    $this->manager = $manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('blazy.manager'), $container
      ->get('gridstack.manager'));
  }

  /**
   * Returns the gridstack admin.
   */
  public function admin() {
    return \Drupal::service('gridstack.admin');
  }

  /**
   * Returns the gridstack manager.
   */
  public function manager() {
    return $this->manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = [
      'stamp' => [
        'default' => [],
      ],
    ];
    foreach (GridStackDefault::extendedSettings() as $key => $value) {
      $options[$key] = [
        'default' => $value,
      ];
    }
    return $options + parent::defineOptions();
  }

  /**
   * Overrides parent::buildOptionsForm().
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $fields = [
      'captions',
      'layouts',
      'images',
      'links',
      'titles',
      'classes',
    ];
    $definition = $this
      ->getDefinedFieldOptions($fields);
    $this
      ->admin()
      ->buildSettingsForm($form, $definition);
    $count = count($definition['captions']);
    $wide = $count > 2 ? ' form--wide form--caption-' . $count : ' form--caption-' . $count;
    $title = '<p class="form__header form__title">';
    $title .= $this
      ->t('Check Vanilla if using content/custom markups, not fields. <small>See it under <strong>Format > Show</strong> section. Otherwise Gridstack markups apply which require some fields added below.</small>');
    $title .= '</p>';
    $form['opening']['#markup'] = '<div class="form--gridstack form--slick form--views form--half form--vanilla has-tooltip' . $wide . '">' . $title;
    $form['image']['#description'] .= ' ' . $this
      ->t('Be sure to UNCHECK "Use field template" (by default already UNCHECKED) to have it work for Blazy lazyloading. Use Blazy formatters for relevant features such as Colorbox/Photobox/Photoswipe, or multimedia supports.');
  }

  /**
   * Overrides StylePluginBase::render().
   */
  public function render() {
    $blazy = $this
      ->blazyManager();
    $view = $this->view;
    $settings = $this->options + GridStackDefault::entitySettings();
    $view_name = $view->storage
      ->id();
    $view_mode = $view->current_display;
    $count = count($view->result);
    $id = Blazy::getHtmlId("gridstack-{$view_name}-{$view_mode}", $settings['id']);
    $optionset = GridStack::load($settings['optionset']);
    $grids = $optionset
      ->getEndBreakpointGrids();
    $settings += [
      'cache_metadata' => [
        'keys' => [
          $id,
          $view_mode,
          $settings['optionset'],
        ],
      ],
      'count' => $count,
      'current_view_mode' => $view_mode,
      'view_name' => $view_name,
    ];

    // Grids: x y width height image_style
    // Breakpoints: xs sm md lg, may contain width column image_style grids.
    $settings['id'] = $id;
    $settings['item_id'] = 'box';
    $settings['caption'] = array_filter($settings['caption']);
    $settings['namespace'] = 'gridstack';
    $settings['ratio'] = '';
    $settings['_views'] = TRUE;

    // Converts gridstack breakpoint grids from stored JSON into array.
    $optionset
      ->gridsJsonToArray($settings);
    $elements = [];
    foreach ($this
      ->renderGrouping($view->result, $settings['grouping']) as $rows) {
      $settings = array_filter($settings);
      $items = $this
        ->buildElements($settings, $rows, $grids);

      // Supports Blazy formatter multi-breakpoint images if available.
      $blazy
        ->isBlazy($settings, $items[0]);
      $build = [
        'items' => $items,
        'optionset' => $optionset,
        'settings' => $settings,
      ];
      $elements = $this->manager
        ->build($build);
      unset($build);
    }
    return $elements;
  }

  /**
   * Returns gridstack contents.
   */
  public function buildElements(array $settings, $rows, $grids = []) {
    $build = [];
    $view = $this->view;
    $item_id = $settings['item_id'];
    foreach ($rows as $delta => $row) {
      $view->row_index = $delta;
      $box = [];
      $box['delta'] = $delta;
      $box[$item_id] = [];

      // Overrides fallback breakpoint image_style with grid image_style.
      if (!empty($settings['breakpoints'])) {
        foreach ($settings['breakpoints'] as $key => &$breakpoint) {
          if (isset($breakpoint['image_style']) && !empty($breakpoint['grids'][$delta]) && !empty($breakpoint['grids'][$delta]['image_style'])) {
            $breakpoint['image_style'] = $breakpoint['grids'][$delta]['image_style'];
          }

          // Overrides image style to use a defined image style per grid item.
          // This allows each individual box to have different image style.
          if ($key == 'lg' && !empty($breakpoint['grids'][$delta]['image_style'])) {
            $settings['_dimensions'] = empty($settings['background']);
            $settings['image_style'] = $breakpoint['grids'][$delta]['image_style'];
          }
        }
      }
      $box['settings'] = $settings;
      if (!empty($settings['class'])) {
        $classes = $this
          ->getFieldString($row, $settings['class'], $delta);
        $box['settings']['class'] = empty($classes[$delta]) ? [] : $classes[$delta];
      }

      // Use Vanilla gridstack if so configured, ignoring GridStack markups.
      if (!empty($settings['vanilla'])) {
        $box[$item_id] = $view->rowPlugin
          ->render($row);
      }
      else {

        // Build individual row/element contents.
        $this
          ->buildElement($box, $row, $delta);
      }

      // Build gridstack items.
      $build[] = $box;
      unset($box);
    }
    unset($view->row_index);
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GridStackViews::$manager protected property The gridstack service manager.
GridStackViews::admin public function Returns the gridstack admin.
GridStackViews::buildElements public function Returns gridstack contents.
GridStackViews::buildOptionsForm public function Overrides parent::buildOptionsForm().
GridStackViews::create public static function
GridStackViews::defineOptions protected function
GridStackViews::manager public function Returns the gridstack manager.
GridStackViews::render public function Overrides StylePluginBase::render().
GridStackViews::__construct public function Constructs a GridStackManager object.