DynamicLayoutSettings.php in Dynamic Layouts 8
File
src/Entity/DynamicLayoutSettings.php
View source
<?php
namespace Drupal\dynamic_layouts\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\dynamic_layouts\DynamicLayoutSettingsInterface;
use Drupal\dynamic_layouts\DynamicLayoutConstants as Constants;
class DynamicLayoutSettings extends ConfigEntityBase implements DynamicLayoutSettingsInterface {
public $id;
public $label;
public $settings;
public function getSettings() {
return $this->settings;
}
public function getFrontendLibrary() {
$settings = unserialize($this->settings);
return isset($settings[Constants::FRONTEND_LIBRARY]) ? $settings[Constants::FRONTEND_LIBRARY] : NULL;
}
public function setFrontendLibrary($frontend_library) {
$settings = unserialize($this->settings);
$settings[Constants::FRONTEND_LIBRARY] = $frontend_library;
$this->settings = serialize($settings);
}
public function getColumnPrefix() {
$settings = unserialize($this->settings);
$column_prefix = NULL;
if (isset($settings[Constants::COLUMN_PREFIX])) {
$column_prefix = $settings[Constants::COLUMN_PREFIX];
}
if ($this
->getFrontendLibrary() == Constants::BOOTSTRAP) {
$column_prefix = 'col';
}
return $column_prefix;
}
public function setColumnPrefix($column_prefix) {
$settings = unserialize($this->settings);
$settings[Constants::COLUMN_PREFIX] = $column_prefix;
$this->settings = serialize($settings);
}
public function getGridColumnCount() {
$settings = unserialize($this->settings);
$column_count = NULL;
if (isset($settings[Constants::GRID_COLUMN_COUNT])) {
$column_count = $settings[Constants::GRID_COLUMN_COUNT];
}
if ($this
->getFrontendLibrary() == Constants::BOOTSTRAP) {
$column_count = 12;
}
return $column_count;
}
public function setGridColumnCount($grid_column_count) {
$settings = unserialize($this->settings);
$settings[Constants::GRID_COLUMN_COUNT] = $grid_column_count;
$this->settings = serialize($settings);
}
public function getFrontendColumnClasses() {
$frontend_library = $this
->getFrontendLibrary();
switch ($frontend_library) {
case 'custom':
$column_classes = $this
->getCustomColumnClasses();
break;
case Constants::BOOTSTRAP:
default:
$column_classes = $this
->getBootstrapColumnClasses();
break;
}
return $column_classes;
}
public function purgeColumnWidthNumbers($last_column_number, $new_column_prefix = '') {
if (!($layout_config_entities = \Drupal::entityTypeManager()
->getStorage('dynamic_layout')
->loadMultiple())) {
return NULL;
}
foreach ($layout_config_entities as $layout_config_entity) {
$rows = $layout_config_entity
->getRows();
if ($rows) {
foreach ($rows as $row_key => $row) {
$columns = $row[Constants::COLUMNS];
foreach ($columns as $column_key => $column) {
$rows[$row_key][Constants::COLUMNS][$column_key]['column_width_number'] = $last_column_number;
if ($new_column_prefix) {
$rows[$row_key][Constants::COLUMNS][$column_key]['column_width_prefix'] = $new_column_prefix;
}
if (isset($rows[$row_key][Constants::COLUMNS][$column_key]['custom_column_width_number'])) {
unset($rows[$row_key][Constants::COLUMNS][$column_key]['custom_column_width_number']);
}
}
}
}
$layout_config_entity->regions = serialize($rows);
$layout_config_entity
->save();
}
}
public function getLastColumnNumber($frontend_library, $new_column_prefix, $new_grid_column_count) {
$last_column_class = '';
switch ($frontend_library) {
case 'custom':
$column_classes = $this
->getCustomColumnClasses($new_grid_column_count);
break;
case Constants::BOOTSTRAP:
default:
$column_classes = $this
->getBootstrapColumnClasses();
break;
}
if ($column_classes) {
$last_column_class = end($column_classes);
}
return $last_column_class;
}
private function getBootstrapColumnClasses() {
$bootstrap_column_widths = [];
for ($i = 1; $i <= 12; $i++) {
$bootstrap_column_widths[$i] = $i;
}
return $bootstrap_column_widths;
}
private function getCustomColumnClasses($new_grid_column_count = '') {
$grid_column_count = $this
->getGridColumnCount();
if ($new_grid_column_count) {
$grid_column_count = $new_grid_column_count;
}
$grid_column_count = intval($grid_column_count);
$column_classes = [];
for ($i = 1; $i <= $grid_column_count; $i++) {
$column_class = $i;
$column_classes[$column_class] = $column_class;
}
return $column_classes;
}
}