You are here

public function EditColumnModalForm::buildForm in Dynamic Layouts 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/EditColumnModalForm.php, line 67

Class

EditColumnModalForm
EditColumnModalForm class.

Namespace

Drupal\dynamic_layouts\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
  $entity_id = \Drupal::request()
    ->get(Constants::ENTITY_ID);
  if ($entity_id === NULL) {
    $entity_id = \Drupal::request()
      ->get(Constants::DYNAMIC_LAYOUT_ID);
  }
  $column_id = \Drupal::request()
    ->get(Constants::COLUMN_ID);
  $row_id = \Drupal::request()
    ->get(Constants::ROW_ID);
  $column_classes = '';
  $column_name = '';
  $column_width_number = '';

  /* @var \Drupal\dynamic_layouts\DynamicLayoutInterface $config_entity */
  if ($entity_id !== NULL && ($config_entity = $this->entityTypeManager
    ->getStorage('dynamic_layout')
    ->load($entity_id))) {
    $column_classes = $config_entity
      ->getColumnClasses($row_id, $column_id);
    $column_name = $config_entity
      ->getColumnName($row_id, $column_id);
    $column_width_number = $config_entity
      ->getColumnWidthNumber($row_id, $column_id);
  }
  $form['#prefix'] = '<div id="modal_form">';
  $form['#suffix'] = '</div>';

  // The status messages that will contain any form errors.
  $form['status_messages'] = [
    '#type' => 'status_messages',
    '#weight' => -10,
  ];

  // Row classes textarea.
  $form[Constants::ENTITY_ID] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Entity ID'),
    '#default_value' => $entity_id,
    '#disabled' => TRUE,
  ];

  // Row id disabled textfield.
  $form[Constants::ROW_ID] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Row id'),
    '#default_value' => $row_id,
    '#disabled' => TRUE,
  ];

  // Column id disabled textfield.
  $form[Constants::COLUMN_ID] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Column id'),
    '#default_value' => $column_id,
    '#disabled' => TRUE,
  ];

  /** @var \Drupal\dynamic_layouts\DynamicLayoutSettingsInterface $settings */
  if ($settings = $this->entityTypeManager
    ->getStorage('dynamic_layout_settings')
    ->load('settings')) {
    $column_class_options = $settings
      ->getFrontendColumnClasses();

    // Column classes textarea.
    $form['column_width_number'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Column width class'),
      '#description' => $this
        ->t('Choose your class for the width of this column, these classes are based on the Dynamic Layouts settings.'),
      '#options' => $column_class_options,
      '#default_value' => $column_width_number,
    ];
  }

  // Column name textfield.
  $form['column_name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Column name'),
    '#default_value' => $column_name,
  ];

  // Column classes textarea.
  $form['custom_column_classes'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Custom column classes'),
    '#description' => $this
      ->t('Fill in your column classes, separated by a comma. E.g: "class1, class2, class3"'),
    '#default_value' => $column_classes,
  ];
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['send'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
    '#attributes' => [
      'class' => [
        'use-ajax',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'submitModalFormAjax',
      ],
      'event' => 'click',
    ],
  ];
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  return $form;
}