You are here

GridStackHook.php in GridStack 8.2

Namespace

Drupal\gridstack

File

src/GridStackHook.php
View source
<?php

namespace Drupal\gridstack;

use Drupal\Core\Layout\LayoutDefinition;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\blazy\Blazy;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides GridStack utility methods for Drupal hooks.
 */
class GridStackHook {
  use StringTranslationTrait;

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

  /**
   * Constructs a GridStack object.
   *
   * @param \Drupal\gridstack\GridStackManagerInterface $manager
   *   The gridstack manager service.
   */
  public function __construct(GridStackManagerInterface $manager) {
    $this->manager = $manager;
  }

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

  /**
   * Implements hook_config_schema_info_alter().
   *
   * @todo: Also verify widget.module, and revisit if any further change.
   * @todo replace with BlazyAlter::configSchemaInfoAlter() post blazy:8.x-2.0.
   */
  public function configSchemaInfoAlter(array &$definitions) {
    if (isset($definitions['layout_plugin.settings'])) {
      $this
        ->mapConfigSchemaInfoAlter($definitions['layout_plugin.settings']);
    }

    // @todo: Remove when DS passes layout.settings to layout_plugin.settings.
    if (isset($definitions['core.entity_view_display.*.*.*.third_party.ds'])) {
      $this
        ->mapConfigSchemaInfoAlter($definitions['core.entity_view_display.*.*.*.third_party.ds']['mapping']['layout']['mapping']['settings']);
    }
    foreach ([
      'gridstack_base',
      'gridstack_vanilla',
    ] as $key) {
      if (isset($definitions[$key])) {
        Blazy::configSchemaInfoAlter($definitions, $key, GridStackDefault::extendedSettings());
      }
    }
  }

  /**
   * Maps config schema.
   */
  public function mapConfigSchemaInfoAlter(array &$mappings, $source = '') {
    foreach (GridStackDefault::layoutSettings() as $key => $value) {
      if (is_array($value)) {
        $mappings['mapping'][$key]['type'] = 'sequence';
        $mappings['mapping'][$key]['label'] = $key;
        $mappings['mapping'][$key]['sequence'][0]['type'] = 'mapping';
        $mappings['mapping'][$key]['sequence'][0]['label'] = $key;
      }
      else {
        $mappings['mapping'][$key]['type'] = 'string';
        $mappings['mapping'][$key]['label'] = ucwords($key);
      }
    }
    foreach (GridStackDefault::regionSettings() as $key => $value) {
      if (is_array($value)) {
        $mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['type'] = 'sequence';
        $mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['label'] = $key;
        $mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['sequence'][0]['type'] = 'mapping';
        $mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['sequence'][0]['label'] = $key;
      }
      else {
        $mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['type'] = 'string';
        $mappings['mapping']['regions']['sequence'][0]['mapping'][$key]['label'] = ucwords($key);
      }
    }
  }

  /**
   * Implements hook_library_info_alter().
   */
  public function libraryInfoAlter(&$libraries, $extension) {
    if ($path = \gridstack_libraries_get_path('gridstack')) {
      $libraries['gridstack']['js'] = [
        '/' . $path . '/dist/gridstack.min.js' => [
          'weight' => -2,
        ],
      ];
    }
    if (isset($libraries['backbone'])) {
      $libraries['backbone']['dependencies'][] = $this->manager
        ->configLoad('dev', 'gridstack.settings') ? 'gridstack/dev' : 'gridstack/all';
    }
    if (isset($libraries['load'])) {
      $libraries['load']['dependencies'][] = $this->manager
        ->configLoad('gridstatic', 'gridstack.settings') ? 'gridstack/gridstatic' : 'gridstack/gridstack';
    }
  }

  /**
   * Implements hook_field_formatter_info_alter().
   */
  public function fieldFormatterInfoAlter(array &$info) {
    $common = [
      'quickedit' => [
        'editor' => 'disabled',
      ],
      'provider' => 'gridstack',
    ];

    // Supports Media Entity via VEM within VEF if available.
    // @todo drop for blazy 2.x with core Media.
    if ($this->manager
      ->getModuleHandler()
      ->moduleExists('video_embed_media')) {
      $info['gridstack_file'] = $common + [
        'id' => 'gridstack_file',
        'label' => $this
          ->t('GridStack Image with VEF (deprecated)'),
        'description' => $this
          ->t('Display the images associated to VEM/ME as a simple mix of GridStack image/video (deprecated for GridStack Media).'),
        'class' => 'Drupal\\gridstack\\Plugin\\Field\\FieldFormatter\\GridStackFileFormatter',
        'field_types' => [
          'entity_reference',
          'image',
        ],
      ];
    }
    if ($this->manager
      ->getModuleHandler()
      ->moduleExists('paragraphs')) {
      $info['gridstack_paragraphs'] = $common + [
        'id' => 'gridstack_paragraphs',
        'label' => $this
          ->t('GridStack Paragraphs'),
        'description' => $this
          ->t('Display the Paragraphs as a GridStack.'),
        'class' => 'Drupal\\gridstack\\Plugin\\Field\\FieldFormatter\\GridStackParagraphsFormatter',
        'field_types' => [
          'entity_reference_revisions',
        ],
      ];
    }
  }

  /**
   * Implements hook_layout_alter().
   */
  public function layoutAlter(&$definitions) {
    $optionsets = $this->manager
      ->entityLoadMultiple('gridstack');
    $excluded = $this->manager
      ->configLoad('excludes', 'gridstack.settings');
    $framework = $this->manager
      ->configLoad('framework', 'gridstack.settings');
    $path = \drupal_get_path('module', 'gridstack');
    $excludes = [
      'default',
    ];
    if (!empty($excluded)) {
      $excludes = array_unique(array_merge($excludes, array_map('trim', explode(",", $excluded))));
    }
    foreach ($optionsets as $key => $optionset) {
      if (in_array($key, $excludes)) {
        continue;
      }
      $static = !empty($framework) && $optionset
        ->getOption('use_framework');
      $id = $optionset
        ->id();
      $layout_id = GridStackDefault::layoutId($id);
      $regions = $optionset
        ->prepareRegions();

      // Defines the layout.
      $definition = [
        'label' => $optionset
          ->label(),
        'category' => $static ? 'GridStack ' . ucwords($framework) : 'GridStack JS',
        'class' => '\\Drupal\\gridstack\\Plugin\\Layout\\GridStackLayout',
        'default_region' => 'gridstack_0',
        'icon' => $optionset
          ->getIconUrl(),
        'id' => $layout_id,
        'provider' => 'gridstack',
        'additional' => [
          'optionset' => $id,
        ],
        'regions' => $regions,
        'theme_hook' => 'gridstack',
        'path' => $path,
        'library' => 'gridstack/layout',
        'config_dependencies' => [
          'config' => [
            'gridstack.optionset.' . $id,
          ],
          'module' => [
            'gridstack',
          ],
        ],
      ];
      $definitions[$layout_id] = new LayoutDefinition($definition);
    }
  }

}

Classes

Namesort descending Description
GridStackHook Provides GridStack utility methods for Drupal hooks.