You are here

GridStackBase.php in GridStack 8.2

File

src/Entity/GridStackBase.php
View source
<?php

namespace Drupal\gridstack\Entity;

use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Random;
use Drupal\Core\Config\Entity\ConfigEntityBase;

/**
 * Defines the base class for GridStack configuration entity.
 *
 * @see \Drupal\gridstack\Entity\GridStack
 * @see \Drupal\outlayer\Entity\Outlayer
 */
abstract class GridStackBase extends ConfigEntityBase implements GridStackBaseInterface {

  /**
   * The legacy CTools ID for the configurable optionset.
   *
   * @var string
   */
  protected $name;

  /**
   * The human-readable name for the optionset.
   *
   * @var string
   */
  protected $label;

  /**
   * The administrative description.
   *
   * @var string
   */
  protected $description = '';

  /**
   * The weight to re-arrange the order of gridstack optionsets.
   *
   * @var int
   */
  protected $weight = 0;

  /**
   * The plugin instance json to reduce frontend logic.
   *
   * @var string
   */
  protected $json = '';

  /**
   * The plugin instance options.
   *
   * @var array
   */
  protected $options = [];

  /**
   * Overrides Drupal\Core\Entity\Entity::id().
   */
  public function id() {
    return $this->name;
  }

  /**
   * Return description.
   */
  public function description() {
    return $this->description;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return self::load('default')
      ->getOptions('settings');
  }

  /**
   * {@inheritdoc}
   */
  public function getOptions($group = NULL, $property = NULL) {
    $default = self::load('default');
    $default_options = $default ? $default->options : [];
    $options = $this->options ? NestedArray::mergeDeep($default_options, $this->options) : $default_options;
    if ($group) {
      if (is_array($group)) {
        return NestedArray::getValue($options, $group);
      }
      elseif (isset($property) && isset($options[$group])) {
        return isset($options[$group][$property]) ? $options[$group][$property] : NULL;
      }
      return isset($options[$group]) ? $options[$group] : $options;
    }
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function setOptions(array $options = [], $merged = TRUE) {
    $this->options = $merged ? NestedArray::mergeDeep($this->options, $options) : $options;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getOption($group, $default = NULL) {

    // Makes sure to not call ::getOptions($group), else everything is dumped.
    return isset($this
      ->getOptions()[$group]) ? $this
      ->getOptions()[$group] : $default;
  }

  /**
   * {@inheritdoc}
   */
  public function setOption($group, $value) {
    $value = $group == 'settings' && isset($this->options[$group]) ? array_merge($this->options[$group], $value) : $value;
    $this->options[$group] = $value;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getJson($group = '') {
    $default = self::load('default');
    if ($group) {
      $defaults = isset($default->json[$group]) ? $default->json[$group] : '';
      return isset($this->json[$group]) ? $this->json[$group] : $defaults;
    }
    return $this->json;
  }

  /**
   * {@inheritdoc}
   */
  public function setJson($group, $value) {
    $this->json[$group] = $value;
    return $this;
  }

  /**
   * Load the optionset with a fallback.
   */
  public static function loadWithFallback($id) {
    $optionset = self::load($id);

    // Ensures deleted optionset while being used doesn't screw up.
    return empty($optionset) ? self::load('default') : $optionset;
  }

  /**
   * Returns the file icon URI to be stored in public directory.
   */
  public function buildIconFileUri($dir = 'gridstack', $extension = 'png') {
    return \file_build_uri($dir . '/' . $this
      ->id() . '.' . $extension);
  }

  /**
   * Returns the file icon URI stored in public directory.
   */
  public function getIconFileUri($dir = 'gridstack', $extension = 'png') {
    $uri = $this
      ->buildIconFileUri($dir, $extension);
    return is_file($uri) ? $uri : FALSE;
  }

  /**
   * Returns a random name.
   */
  public function randomize($rand = 4) {
    $random = new Random();
    return mb_strtolower($random
      ->name($rand));
  }

  /**
   * Randomize a label from ID.
   */
  public function getLabelFromId($label) {
    $names = strpos($label, '__') === FALSE ? explode("_", $label) : explode("__", $label);
    return implode(" ", $names);
  }

  /**
   * Returns a randomized ID.
   */
  public function getRandomizedId($rand = 4) {
    $id = $this
      ->id();
    $names = strpos($id, '__') === FALSE ? explode("_", $id) : explode("__", $id);
    if (count($names) > 1) {
      array_pop($names);
    }
    return implode("__", $names) . '__' . $this
      ->randomize($rand);
  }

}

Classes

Namesort descending Description
GridStackBase Defines the base class for GridStack configuration entity.