You are here

public function DynamicLayout::addStartingRows in Dynamic Layouts 8

Add multiple rows to the layout.

Parameters

int $rows_count: The amount of rows to add.

Overrides DynamicLayoutInterface::addStartingRows

File

src/Entity/DynamicLayout.php, line 414

Class

DynamicLayout
Defines the DynamicLayout entity.

Namespace

Drupal\dynamic_layouts\Entity

Code

public function addStartingRows($general_settings) {
  $rows_count = 1;
  if (isset($general_settings['start_rows_count'])) {
    $rows_count = $general_settings['start_rows_count'];
  }

  // Set the default column class for the starting rows.
  $starting_default_column_class = '';
  if (isset($general_settings[Constants::DEFAULT_COLUMN_CLASS]) && $general_settings[Constants::DEFAULT_COLUMN_CLASS]) {
    $starting_default_column_class = \trim($general_settings[Constants::DEFAULT_COLUMN_CLASS]);
    $this->default_column_class = $starting_default_column_class;
  }

  // Set the default row class for the starting rows.
  $starting_default_row_class = '';
  if (isset($general_settings[Constants::DEFAULT_ROW_CLASS]) && $general_settings[Constants::DEFAULT_ROW_CLASS]) {
    $starting_default_row_class = \trim($general_settings[Constants::DEFAULT_ROW_CLASS]);
    $this->default_row_class = $starting_default_row_class;
  }

  /** @var \Drupal\dynamic_layouts\DynamicLayoutSettingsInterface $settings */
  $last_column_class = '';
  $column_width_prefix = '';
  if ($settings = \Drupal::entityTypeManager()
    ->getStorage(Constants::DYNAMIC_LAYOUT_SETTINGS)
    ->load(Constants::SETTINGS)) {
    $column_width_prefix = $settings
      ->getColumnPrefix();
    $column_classes = array_keys($settings
      ->getFrontendColumnClasses());
    $last_column_class = end($column_classes);
  }

  // Add new rows.
  $rows = [];
  for ($i = 1; $i <= $rows_count; $i++) {
    $row_id = uniqid();
    $column_id = uniqid();
    $columns = [];
    $edit_column = $this
      ->getEditColumnLink($this
      ->id(), $row_id, $column_id);
    $delete_column = $this
      ->getDeleteColumnLink($this
      ->id(), $row_id, $column_id);
    $columns[] = [
      Constants::EDIT_COLUMN => $edit_column,
      Constants::DELETE_COLUMN => $delete_column,
      Constants::COLUMN_ID => $column_id,
      Constants::COLUMN_WIDTH_NUMBER => $last_column_class,
      Constants::COLUMN_WIDTH_PREFIX => $column_width_prefix,
      Constants::DEFAULT_COLUMN_CLASS => $starting_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,
      ],
    ];
    $rows[] = [
      Constants::ROW_ID => $row_id,
      Constants::DEFAULT_ROW_CLASS => $starting_default_row_class,
      'admin_row_classes' => [
        'dynamic-layout-row',
      ],
      Constants::CUSTOM_ROW_CLASSES => [],
      Constants::COLUMNS => $columns,
    ];
  }
  $this->regions = serialize($rows);
  $this
    ->save();
}