You are here

class GridStackManager in GridStack 8.2

Same name and namespace in other branches
  1. 8 src/GridStackManager.php \Drupal\gridstack\GridStackManager

Implements GridStackManagerInterface.

Hierarchy

Expanded class hierarchy of GridStackManager

1 string reference to 'GridStackManager'
gridstack.services.yml in ./gridstack.services.yml
gridstack.services.yml
1 service uses GridStackManager
gridstack.manager in ./gridstack.services.yml
Drupal\gridstack\GridStackManager

File

src/GridStackManager.php, line 15

Namespace

Drupal\gridstack
View source
class GridStackManager extends BlazyManagerBase implements GridStackManagerInterface {

  /**
   * The GridStack layout engine.
   *
   * @var \Drupal\gridstack\GridStackEnginePluginInterface
   */
  protected $engine;

  /**
   * The GridStack layout engine plugin ID.
   *
   * @var string
   */
  protected $engineId;

  /**
   * The GridStack optionset.
   *
   * @var \Drupal\gridstack\Entity\GridStack
   */
  protected $optionset;

  /**
   * The GridStack layout engine manager service.
   *
   * @var \Drupal\gridstack\GridStackEngineManagerInterface
   */
  protected $engineManager;

  /**
   * The GridStack skin manager service.
   *
   * @var \Drupal\gridstack\GridStackSkinManagerInterface
   */
  protected $skinManager;

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

  /**
   * The gridstack layout CSS classes.
   *
   * @var array
   */
  protected $mergedClasses;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance
      ->setEngineManager($container
      ->get('gridstack.engine_manager'));
    $instance
      ->setSkinManager($container
      ->get('gridstack.skin_manager'));
    $instance
      ->setStylizer($container
      ->get('gridstack.stylizer'));
    return $instance;
  }

  /**
   * Returns GridStack layout engine manager service.
   */
  public function engineManager() {
    return $this->engineManager;
  }

  /**
   * Sets GridStack layout engine manager service.
   */
  public function setEngineManager(GridStackEngineManagerInterface $engine_manager) {
    $this->engineManager = $engine_manager;
    return $this;
  }

  /**
   * Returns GridStack skin manager service.
   */
  public function skinManager() {
    return $this->skinManager;
  }

  /**
   * Sets GridStack skin manager service.
   */
  public function setSkinManager(GridStackSkinManagerInterface $skin_manager) {
    $this->skinManager = $skin_manager;
    return $this;
  }

  /**
   * Returns GridStack stylizer service.
   */
  public function stylizer() {
    return $this->stylizer;
  }

  /**
   * Sets GridStack stylizer service.
   */
  public function setStylizer(GridStackStylizerInterface $stylizer) {
    $this->stylizer = $stylizer;
    return $this;
  }

  /**
   * Returns GridStack ayout engine plugin ID.
   */
  public function getEngineId() {
    if (!isset($this->engineId)) {
      $this->engineId = 'gridstack_js';
    }
    return $this->engineId;
  }

  /**
   * Sets GridStack layout engine plugin ID.
   */
  protected function setEngineId($engine_id) {
    $this->engineId = $engine_id;
    return $this;
  }

  /**
   * Returns the layout engine.
   */
  public function getEngine(array $settings, $engine = NULL) {
    if (!isset($this->engine) || $engine) {
      if (empty($engine)) {
        $engine = empty($settings['_engine']) ? $this
          ->getEngineId() : $settings['_engine'];
      }
      $this->engine = $this->engineManager
        ->load($engine, $settings);
    }
    return $this->engine;
  }

  /**
   * Sets the layout engine.
   */
  protected function setEngine(array $settings) {
    $this->engine = $this->engineManager
      ->load($settings['_engine'], $settings);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'preRenderGridStack',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function attach(array $attach = []) {
    $load = parent::attach($attach);
    if (isset($this->engineManager)) {
      $this->engineManager
        ->attach($load, $attach);
      $this->skinManager
        ->attach($load, $attach);
      $this->stylizer
        ->attach($load, $attach);
    }
    $this->moduleHandler
      ->alter('gridstack_attach', $load, $attach);
    return $load;
  }

  /**
   * {@inheritdoc}
   */
  public function getMergedClasses($flatten = FALSE) {
    if (!isset($this->mergedClasses[$flatten])) {
      $classes = $this->engineManager
        ->getClassOptions();
      $this->mergedClasses[$flatten] = $this->stylizer
        ->getMergedClasses($flatten, $classes);
    }
    return $this->mergedClasses[$flatten];
  }

  /**
   * Returns the common settings inherited down to each item.
   */
  protected function getGridStackSettings() {
    return array_intersect_key($this
      ->configLoad('', 'gridstack.settings'), GridStackDefault::cleanUiSettings());
  }

  /**
   * Build the HTML settings.
   */
  public function prepareSettings(array &$settings) {
    $settings += array_merge(GridStackDefault::htmlSettings(), $this
      ->getGridStackSettings());

    // The use_nested is not for js or native Grid, _yet, but CSS framework.
    $settings['column'] = $this->optionset
      ->getSetting('column', 12);
    $settings['use_nested'] = $settings['use_framework'] = !empty($settings['framework']) && $this->optionset
      ->getOption('use_framework');
    $settings['gridnative'] = !$settings['use_framework'] && !empty($settings['gridnative']);
    $settings['_gridstack'] = TRUE;

    // Provides configurable layout engines.
    if ($settings['use_framework']) {
      $settings['background'] = $settings['_gridstack'] = $settings['use_js'] = FALSE;
      $settings['_engine'] = $settings['framework'];
    }
    else {

      // Provides programmatic layout engines.
      $settings['_gridstack'] = empty($settings['ungridstack']);
      $fallback = $settings['gridnative'] ? 'gridstack_native' : 'gridstack_js';
      $settings['_engine'] = empty($settings['_engine']) ? $fallback : $settings['_engine'];
    }
    $settings['id'] = Blazy::getHtmlId('gridstack-' . $settings['optionset'] . '-' . $settings['_engine'], $settings['id']);
    $this
      ->setEngineId($settings['_engine']);
  }

  /**
   * Prepares the layout engine.
   */
  protected function initEngine(array &$build, array &$element) {
    $settings =& $build['settings'];
    $this
      ->setEngine($settings);
    $this->engine
      ->build($build, $element);
  }

  /**
   * Returns the wrapper attributes, empty to allow overrides.
   */
  public function prepareAttributes(array &$build) {
    return [];
  }

  /**
   * Prepares GridStack build.
   */
  protected function prepareBuild(array &$build, array &$element) {

    // Just in case removed or changed to data-layout-builder-BLAH.
    $id1 = 'data-layout-update-url';
    $id2 = 'data-layout-builder-highlight-id';
    $settings =& $build['settings'];

    // Provides layout editor settings.
    $settings['_lb'] = isset($element['#attributes']) && (isset($element['#attributes'][$id1]) || isset($element['#attributes'][$id2]));
    $settings['_lbux'] = $settings['_lb'] && $this->moduleHandler
      ->moduleExists('lb_ux');
    $settings['_panels'] = isset($element['#prefix']) && strpos($element['#prefix'], 'panels-ipe-content') !== FALSE;
    $settings['_ipe'] = $settings['_lb'] || $settings['_lbux'] || $settings['_panels'];

    // Supports Blazy multi-breakpoint images if provided.
    if (!empty($settings['check_blazy']) && !empty($build['items'][0])) {
      $this
        ->isBlazy($settings, $build['items'][0]);
    }

    // Provides the optionset if empty. GridStackLayout, or GridStackViews, may
    // already set this. GridStackFormatter used to set it, too.
    if (empty($build['optionset'])) {
      $build['optionset'] = GridStack::loadWithFallback($settings['optionset']);
    }
    $this->optionset = $build['optionset'];

    // Prepares the settings.
    $this
      ->prepareSettings($settings);
    $this
      ->initEngine($build, $element);
  }

  /**
   * {@inheritdoc}
   */
  public function build(array $build = []) {
    foreach ([
      'attached',
      'layout',
      'variant',
    ] + GridStackDefault::themeProperties() as $key) {
      $build[$key] = isset($build[$key]) ? $build[$key] : [];
    }
    $gridstack = [
      '#theme' => 'gridstack',
      '#build' => $build,
      '#pre_render' => [
        [
          $this,
          'preRenderGridStack',
        ],
      ],
      'items' => [],
      '#layout' => $build['layout'],
    ];
    $this->moduleHandler
      ->alter('gridstack_build', $gridstack, $build['settings']);
    return empty($build['items']) ? [] : $gridstack;
  }

  /**
   * {@inheritdoc}
   */
  public function preRenderGridStack($element) {
    $build = $element['#build'];
    unset($element['#build']);

    // Build GridStack elements.
    $this
      ->prepareBuild($build, $element);
    $settings =& $build['settings'];

    // Provides cache, attributes, assets.
    // @todo reuse ::setAttachments post blazy:2.1+.
    $cache = $this
      ->getCacheMetadata($build);
    $attributes = $this
      ->prepareAttributes($build);
    $attachments = $this
      ->attach($settings);
    $attachments = empty($build['attached']) ? $attachments : NestedArray::mergeDeep($build['attached'], $attachments);

    // Adds the required elements for the template.
    $element['#optionset'] = $this->optionset;
    $element['#settings'] = empty($element['#settings']) ? $settings : NestedArray::mergeDeep($element['#settings'], $settings);
    $element['#postscript'] = empty($element['#postscript']) ? $build['postscript'] : NestedArray::mergeDeep($element['#postscript'], $build['postscript']);
    $element['#attributes'] = empty($element['#attributes']) ? $attributes : NestedArray::mergeDeep($element['#attributes'], $attributes);
    $element['#attached'] = empty($element['#attached']) ? $attachments : NestedArray::mergeDeep($element['#attached'], $attachments);
    $element['#cache'] = empty($element['#cache']) ? $cache : NestedArray::mergeDeep($element['#cache'], $cache);

    // Layout Builder is happy, safe to free up wasted children.
    foreach (Element::children($element) as $child) {
      unset($element[$child]);
    }
    unset($build);
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GridStackManager::$engine protected property The GridStack layout engine.
GridStackManager::$engineId protected property The GridStack layout engine plugin ID.
GridStackManager::$engineManager protected property The GridStack layout engine manager service.
GridStackManager::$mergedClasses protected property The gridstack layout CSS classes.
GridStackManager::$optionset protected property The GridStack optionset.
GridStackManager::$skinManager protected property The GridStack skin manager service.
GridStackManager::$stylizer protected property The GridStack stylizer service.
GridStackManager::attach public function
GridStackManager::build public function Returns a cacheable renderable array of a single gridstack instance. Overrides GridStackManagerInterface::build
GridStackManager::create public static function
GridStackManager::engineManager public function Returns GridStack layout engine manager service.
GridStackManager::getEngine public function Returns the layout engine.
GridStackManager::getEngineId public function Returns GridStack ayout engine plugin ID.
GridStackManager::getGridStackSettings protected function Returns the common settings inherited down to each item.
GridStackManager::getMergedClasses public function
GridStackManager::initEngine protected function Prepares the layout engine.
GridStackManager::prepareAttributes public function Returns the wrapper attributes, empty to allow overrides.
GridStackManager::prepareBuild protected function Prepares GridStack build.
GridStackManager::prepareSettings public function Build the HTML settings.
GridStackManager::preRenderGridStack public function
GridStackManager::setEngine protected function Sets the layout engine.
GridStackManager::setEngineId protected function Sets GridStack layout engine plugin ID.
GridStackManager::setEngineManager public function Sets GridStack layout engine manager service.
GridStackManager::setSkinManager public function Sets GridStack skin manager service.
GridStackManager::setStylizer public function Sets GridStack stylizer service.
GridStackManager::skinManager public function Returns GridStack skin manager service.
GridStackManager::stylizer public function Returns GridStack stylizer service.
GridStackManager::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.