You are here

class RegionMap in Layout Plugin Views 8

Same name and namespace in other branches
  1. 8.2 src/RegionMap.php \Drupal\layout_plugin_views\RegionMap

Hierarchy

Expanded class hierarchy of RegionMap

1 file declares its use of RegionMap
Fields.php in src/Plugin/views/row/Fields.php

File

src/RegionMap.php, line 7

Namespace

Drupal\layout_plugin_views
View source
class RegionMap {

  /**
   * @var \Drupal\layout_plugin_views\Plugin\views\row\Fields
   */
  private $plugin;

  /**
   * @var array
   */
  private $map;

  /**
   * @var \Drupal\layout_plugin_views\FieldsPluginOptions
   */
  private $pluginOptions;
  public function __construct(Fields $plugin, FieldsPluginOptions $plugin_options) {
    $this->plugin = $plugin;
    $this->pluginOptions = $plugin_options;
    $this
      ->generateRegionMap();
  }

  /**
   * Generates the region map.
   */
  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;
    }
  }

  /**
   * Determines if the given field is assigned to an existing region.
   *
   * @param string $field_name
   *
   * @return bool
   */
  private function fieldHasValidAssignment($field_name) {
    return $this
      ->selectedLayoutHasRegion($this->pluginOptions
      ->getAssignedRegion($field_name));
  }

  /**
   * Determines if the given machine name is a region in the selected layout.
   *
   * @param string $region_name
   *
   * @return bool
   */
  private function selectedLayoutHasRegion($region_name) {
    return in_array($region_name, $this
      ->getRegionNamesForSelectedLayout());
  }

  /**
   * Gets the machine names of all regions in the selected layout.
   *
   * @return string[]
   */
  private function getRegionNamesForSelectedLayout() {
    $definition = $this->pluginOptions
      ->getSelectedLayoutDefinition();
    $available_regions = array_keys($definition['region_names']);
    return $available_regions;
  }

  /**
   * Retrieves an array of region machine names for all regions that contain
   * fields to be rendered.
   *
   * @return array
   */
  public function getNonEmptyRegionNames() {
    $non_empty = [];
    foreach ($this->map as $region => $fields) {
      if (!empty($fields)) {
        $non_empty[] = $region;
      }
    }
    return $non_empty;
  }

  /**
   * Returns the fields to be rendered for the given region.
   *
   * @param string $region_name
   *
   * @return \Drupal\views\Plugin\views\field\FieldPluginBase[]
   */
  public function getFieldsForRegion($region_name) {
    return !empty($this->map[$region_name]) ? $this->map[$region_name] : [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RegionMap::$map private property
RegionMap::$plugin private property
RegionMap::$pluginOptions private property
RegionMap::fieldHasValidAssignment private function Determines if the given field is assigned to an existing region.
RegionMap::generateRegionMap public function Generates the region map.
RegionMap::getFieldsForRegion public function Returns the fields to be rendered for the given region.
RegionMap::getNonEmptyRegionNames public function Retrieves an array of region machine names for all regions that contain fields to be rendered.
RegionMap::getRegionNamesForSelectedLayout private function Gets the machine names of all regions in the selected layout.
RegionMap::selectedLayoutHasRegion private function Determines if the given machine name is a region in the selected layout.
RegionMap::__construct public function