3 namespace Drupal\bootstrap_layouts;
5 use Drupal\Component\Utility\DiffArray;
6 use Drupal\Component\Utility\NestedArray;
9 * Class BootstrapLayout.
11 class BootstrapLayout {
21 * The original layout data, used to determine if layout data has changed.
28 * BootstrapLayout constructor.
31 * The layout identifier.
32 * @param array $regions
34 * @param array $settings
35 * The layout settings.
37 * The path to the layout.
39 public function __construct($id, array $regions = [], array $settings = [], $path = NULL) {
42 'regions' => $regions,
43 'settings' => $settings,
46 $this->original = $this->data;
50 * Indicates whether or not the layout data has changed.
55 public function hasChanged() {
56 return !!DiffArray::diffAssocRecursive($this->original, $this->data);
60 * Retrieves the layout identifier.
64 public function getId() {
65 return $this->data['id'];
69 * Retrieves the path to the layout, may not be set.
73 public function getPath() {
74 return $this->data['path'];
78 * Retrieves a specific layout region.
81 * The layout region to retrieve.
82 * @param mixed $default_value
83 * The default value to use if layout region does not exists.
86 * The layout region value or $default_value if it does not exist.
88 public function getRegion($name, $default_value = NULL) {
89 return isset($this->data['regions'][$name]) ? $this->data['regions'][$name] : $default_value;
93 * Retrieves all defined layout regions.
96 * An associative array of layout regions, keyed by their machine name.
98 public function getRegions() {
99 return $this->data['regions'];
103 * Retrieves a specific layout setting.
105 * @param string $name
106 * The layout setting name. Can be dot notation to indicate a deeper key in
107 * the settings array.
108 * @param mixed $default_value
109 * The default value to use if layout setting does not exists.
112 * The layout setting value or $default_value if it does not exist.
114 public function getSetting($name, $default_value = NULL) {
115 $parts = explode('.', $name);
116 if (count($parts) === 1) {
117 return isset($this->data['settings'][$name]) ? $this->data['settings'][$name] : $default_value;
119 $value = NestedArray::getValue($this->data['settings'], $parts, $key_exists);
120 return $key_exists ? $value : NULL;
124 * Retrieves all defined layout settings.
127 * An associative array of layout settings, keyed by their machine name.
129 public function getSettings() {
130 return $this->data['settings'];
134 * Indicates if this layout is a Bootstrap Layouts layout.
139 * @todo This seems backwards, maybe refactor?
141 public function isBootstrapLayout() {
142 static $bootstrap_manager;
143 if (!isset($bootstrap_manager)) {
144 /** @var \Drupal\bootstrap_layouts\BootstrapLayoutsManager $bootstrap_manager */
145 $bootstrap_manager = \Drupal::service('plugin.manager.bootstrap_layouts');
147 return $bootstrap_manager->isBootstrapLayout($this->data['id']);
151 * Sets the layout identifier.
154 * The layout identifier.
156 * @return \Drupal\bootstrap_layouts\BootstrapLayout
157 * The current BootstrapLayout instance.
159 public function setId($id) {
160 $this->data['id'] = $id;
165 * Sets the path to the layout.
167 * @param string $path
168 * The path to the layout.
170 * @return string|null
172 public function setPath($path) {
173 $this->data['path'] = $path;
178 * Sets a specific layout region.
180 * @param string $name
181 * The layout region name.
182 * @param mixed $value
183 * The layout region value.
185 * @return \Drupal\bootstrap_layouts\BootstrapLayout
186 * The current BootstrapLayout instance.
188 public function setRegion($name, $value = NULL) {
189 $this->data['regions'][$name] = $value;
194 * Sets a specific layout setting.
196 * @param string $name
197 * The layout setting name. Can be dot notation to indicate a deeper key in
198 * the settings array.
199 * @param mixed $value
200 * The layout setting value.
202 * @return \Drupal\bootstrap_layouts\BootstrapLayout
203 * The current BootstrapLayout instance.
205 public function setSetting($name, $value = NULL) {
206 $parts = explode('.', $name);
207 if (count($parts) === 1) {
208 $this->data['settings'][$name] = $value;
211 NestedArray::setValue($this->data['settings'], $parts, $value);
217 * Removes a layout region.
219 * @param string $name
220 * The layout region to remove.
223 * The region that was removed.
225 public function unsetRegion($name) {
226 $old = isset($this->data['regions'][$name]) ? $this->data['regions'][$name] : NULL;
227 unset($this->data['regions'][$name]);
232 * Removes a layout setting.
234 * @param string $name
235 * The layout region to remove.
238 * The setting that was removed.
240 public function unsetSetting($name) {
241 $old = isset($this->data['settings'][$name]) ? $this->data['settings'][$name] : NULL;
242 unset($this->data['settings'][$name]);