RegionMap.php in Layout Plugin Views 8.2
File
src/RegionMap.php
View source
<?php
namespace Drupal\layout_plugin_views;
use Drupal\layout_plugin_views\Plugin\views\row\Fields;
class RegionMap {
private $plugin;
private $map;
private $pluginOptions;
public function __construct(Fields $plugin, FieldsPluginOptions $plugin_options) {
$this->plugin = $plugin;
$this->pluginOptions = $plugin_options;
$this
->generateRegionMap();
}
public function generateRegionMap() {
$this->map = [];
foreach ($this->plugin->view->field as $field_name => $field_definition) {
$region_machine_name = $this
->fieldHasValidAssignment($field_name) ? $this->pluginOptions
->getAssignedRegion($field_name) : $this->pluginOptions
->getDefaultRegion();
$this->map[$region_machine_name][$field_name] = $field_definition;
}
}
private function fieldHasValidAssignment($field_name) {
return $this
->selectedLayoutHasRegion($this->pluginOptions
->getAssignedRegion($field_name));
}
private function selectedLayoutHasRegion($region_name) {
return in_array($region_name, $this
->getRegionNamesForSelectedLayout());
}
private function getRegionNamesForSelectedLayout() {
$definition = $this->pluginOptions
->getSelectedLayoutDefinition();
return $definition
->getRegionNames();
}
public function getNonEmptyRegionNames() {
$non_empty = [];
foreach ($this->map as $region => $fields) {
if (!empty($fields)) {
$non_empty[] = $region;
}
}
return $non_empty;
}
public function getFieldsForRegion($region_name) {
return !empty($this->map[$region_name]) ? $this->map[$region_name] : [];
}
}