You are here

public function DynamicLayout::setColumnName in Dynamic Layouts 8

Set the layout column classes.

Parameters

int $row_id: The row number we need to set the classes on.

int $column_id: The column id we need to set the classes on.

string $column_name: The column name.

Overrides DynamicLayoutInterface::setColumnName

File

src/Entity/DynamicLayout.php, line 216

Class

DynamicLayout
Defines the DynamicLayout entity.

Namespace

Drupal\dynamic_layouts\Entity

Code

public function setColumnName($row_id, $column_id, $column_name) {
  $updated_row = $this
    ->getRowById($row_id);
  $columns = $updated_row[Constants::COLUMNS];

  // Set the column name.
  $updated_column = $this
    ->getColumnById($updated_row, $column_id);
  $updated_column[Constants::COLUMN_NAME] = $column_name;
  if (!$column_name) {
    $column_name = 'r' . $row_id . 'c' . $column_id;
  }

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

  // Set the region name.
  $updated_column[Constants::REGION_NAME] = $region_name;

  // Loop over the columns and set the updated column.
  foreach ($columns as $key => $column) {
    if ($column[Constants::COLUMN_ID] == $column_id) {
      $columns[$key] = $updated_column;
    }
  }

  // Loop over the rows and find the corresponding row number.
  $rows = $this
    ->getRows();
  foreach ($rows as $key => $row) {
    if ($row[Constants::ROW_ID] == $row_id) {
      $rows[$key][Constants::COLUMNS] = $columns;
    }
  }
  $this->regions = serialize($rows);
}