You are here

public function DynamicLayout::addColumn in Dynamic Layouts 8

Add a column to a row.

Parameters

int $row_id: The row number we need to add the column to.

Overrides DynamicLayoutInterface::addColumn

File

src/Entity/DynamicLayout.php, line 610

Class

DynamicLayout
Defines the DynamicLayout entity.

Namespace

Drupal\dynamic_layouts\Entity

Code

public function addColumn($row_id) {
  $updated_row = $this
    ->getRowById($row_id);
  $columns = $updated_row[Constants::COLUMNS];
  $column_id = uniqid();
  $column_width_number = '';
  $edit_column = $this
    ->getEditColumnLink($this
    ->id(), $row_id, $column_id);
  $delete_column = $this
    ->getDeleteColumnLink($this
    ->id(), $row_id, $column_id);

  /** @var \Drupal\dynamic_layouts\DynamicLayoutSettingsInterface $settings */
  $column_width_prefix = '';
  if ($settings = \Drupal::entityTypeManager()
    ->getStorage(Constants::DYNAMIC_LAYOUT_SETTINGS)
    ->load(Constants::SETTINGS)) {
    $column_classes = $settings
      ->getFrontendColumnClasses();
    $column_width_number = end($column_classes);
    $column_width_prefix = $settings
      ->getColumnPrefix();
    if ($calculated_column_width_number = $this
      ->calculateColumnWidth($columns, $settings, 'added')) {
      $column_width_number = $calculated_column_width_number;

      // Give each column this column width number.
      foreach ($columns as $key => $column) {
        if (!isset($column[Constants::CUSTOM_COLUMN_WIDTH_NUMBER])) {
          $columns[$key][Constants::COLUMN_WIDTH_NUMBER] = $calculated_column_width_number;
        }
      }
    }
  }
  $columns[] = [
    Constants::EDIT_COLUMN => $edit_column,
    Constants::DELETE_COLUMN => $delete_column,
    Constants::COLUMN_ID => $column_id,
    Constants::COLUMN_WIDTH_NUMBER => $column_width_number,
    Constants::COLUMN_WIDTH_PREFIX => $column_width_prefix,
    Constants::DEFAULT_COLUMN_CLASS => $this->default_column_class,
    Constants::CUSTOM_COLUMN_CLASSES => [],
    Constants::COLUMN_NAME => '',
    Constants::REGION_NAME => 'r' . $row_id . 'c' . $column_id,
    Constants::ADMIN_COLUMN_CLASSES => [
      Constants::DYNAMIC_LAYOUT_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);
}