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;
abstract class GridStackBase extends ConfigEntityBase implements GridStackBaseInterface {
protected $name;
protected $label;
protected $description = '';
protected $weight = 0;
protected $json = '';
protected $options = [];
public function id() {
return $this->name;
}
public function description() {
return $this->description;
}
public static function defaultSettings() {
return self::load('default')
->getOptions('settings');
}
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;
}
public function setOptions(array $options = [], $merged = TRUE) {
$this->options = $merged ? NestedArray::mergeDeep($this->options, $options) : $options;
return $this;
}
public function getOption($group, $default = NULL) {
return isset($this
->getOptions()[$group]) ? $this
->getOptions()[$group] : $default;
}
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;
}
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;
}
public function setJson($group, $value) {
$this->json[$group] = $value;
return $this;
}
public static function loadWithFallback($id) {
$optionset = self::load($id);
return empty($optionset) ? self::load('default') : $optionset;
}
public function buildIconFileUri($dir = 'gridstack', $extension = 'png') {
return \file_build_uri($dir . '/' . $this
->id() . '.' . $extension);
}
public function getIconFileUri($dir = 'gridstack', $extension = 'png') {
$uri = $this
->buildIconFileUri($dir, $extension);
return is_file($uri) ? $uri : FALSE;
}
public function randomize($rand = 4) {
$random = new Random();
return mb_strtolower($random
->name($rand));
}
public function getLabelFromId($label) {
$names = strpos($label, '__') === FALSE ? explode("_", $label) : explode("__", $label);
return implode(" ", $names);
}
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
Name |
Description |
GridStackBase |
Defines the base class for GridStack configuration entity. |