You are here

public function DynamicLayout::getLayoutRegions in Dynamic Layouts 8

Get all layout regions.

Overrides DynamicLayoutInterface::getLayoutRegions

File

src/Entity/DynamicLayout.php, line 709

Class

DynamicLayout
Defines the DynamicLayout entity.

Namespace

Drupal\dynamic_layouts\Entity

Code

public function getLayoutRegions() {
  $rows = $this
    ->getRows();

  // Loop over the rows to gather the layout regions.
  $layout_regions = [];
  $row_count = 1;
  foreach ($rows as $row) {

    // Check if we have columns.
    if (!isset($row[Constants::ROW_ID]) || !isset($row[Constants::COLUMNS])) {
      continue;
    }

    // Loop over the columns from this row.
    $column_count = 1;
    foreach ($row[Constants::COLUMNS] as $column) {
      $region_name = t('Row @row_count - Column @column_count', [
        '@row_count' => $row_count,
        '@column_count' => $column_count,
      ]);
      $region_machine_name = 'r' . $row[Constants::ROW_ID] . 'c' . $column[Constants::COLUMN_ID];
      if (isset($column[Constants::COLUMN_NAME]) && $column[Constants::COLUMN_NAME]) {
        $region_name = $column[Constants::COLUMN_NAME];

        // Convert to machine name.
        $region_machine_name = strtolower($region_name);
        $region_machine_name = preg_replace('/[^a-z0-9_]+/', '_', $region_machine_name);
        $region_machine_name = preg_replace('/_+/', '_', $region_machine_name);

        // Overwrite if we have a region name.
        if (isset($column[Constants::REGION_NAME]) && $column[Constants::REGION_NAME]) {
          $region_machine_name = $column[Constants::REGION_NAME];
        }
      }
      $layout_regions[$region_machine_name] = [
        'label' => $region_name,
      ];
      $column_count++;
    }
    $row_count++;
  }
  return $layout_regions;
}