You are here

public function SettingsForm::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/SettingsForm.php, line 55

Class

SettingsForm
Provides a generic settings form for the DynamicLayouts.

Namespace

Drupal\dynamic_layouts\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\dynamic_layouts\Entity\DynamicLayoutSettings $settings */
  $frontend_library_default = '';
  $column_prefix = '';
  $grid_column_count = '';
  if ($settings = $this->entityTypeManager
    ->getStorage('dynamic_layout_settings')
    ->load('settings')) {
    $frontend_library_default = $settings
      ->getFrontendLibrary();
    $column_prefix = $settings
      ->getColumnPrefix();
    $grid_column_count = $settings
      ->getGridColumnCount();
  }
  if ($frontend_library_default) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('When changing the settings, all configured "Column width classes" will be purged.'));
  }
  $form[Constants::FRONTEND_LIBRARY] = array(
    '#type' => 'select',
    '#title' => t('Frontend library'),
    '#description' => t('Choose which frontend library you want to use for your layout.'),
    '#required' => TRUE,
    '#options' => [
      Constants::BOOTSTRAP => 'Bootstrap (v4)',
      Constants::CUSTOM => 'Custom..',
    ],
    '#default_value' => $frontend_library_default,
  );
  $form['column_prefix'] = array(
    '#type' => 'textfield',
    '#title' => t('Column prefix'),
    '#description' => t('Fill in you column prefix. As an example; Bootstrap uses "col" for prefix. A dash (-) will be added as a suffix.'),
    '#default_value' => $column_prefix,
    '#states' => [
      'visible' => [
        'select[name="frontend_library"]' => [
          'value' => Constants::CUSTOM,
        ],
      ],
      'required' => [
        'select[name="frontend_library"]' => [
          'value' => Constants::CUSTOM,
        ],
      ],
    ],
  );
  $form['grid_column_count'] = array(
    '#type' => 'select',
    '#title' => t('Grid column count'),
    '#description' => t('What is your column count in your grid?'),
    '#options' => [
      '6' => '6 columns grid',
      '8' => '8 columns grid',
      '12' => '12 columns grid',
    ],
    '#default_value' => $grid_column_count,
    '#states' => [
      'visible' => [
        'select[name="frontend_library"]' => [
          'value' => Constants::CUSTOM,
        ],
      ],
      'required' => [
        'select[name="frontend_library"]' => [
          'value' => Constants::CUSTOM,
        ],
      ],
    ],
  );
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#button_type' => 'primary',
  );
  return $form;
}