You are here

public function Settings::buildForm in CKEditor Bootstrap Grid 2.0.x

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 ConfigFormBase::buildForm

File

src/Form/Settings.php, line 58

Class

Settings
Configuration for CKEditor BS Grid.

Namespace

Drupal\ckeditor_bs_grid\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $breakpoints = $this
    ->getBreakpoints();
  $config = $this
    ->configFactory()
    ->get(self::CONFIG_NAME)
    ->get('breakpoints');
  $message = $this
    ->t('Breakpoints and Number of columns can be enabled/disabled per text format in the @link.', [
    '@link' => Link::createFromRoute('editor settings page', 'filter.admin_overview')
      ->toString(),
  ]);
  $form['prefix'] = [
    '#type' => 'markup',
    '#markup' => Markup::create("<div class='messages messages--warning'>" . $message . "</div>"),
    '#weight' => -100,
  ];

  // Column Options. @todo make this configurable.
  $cols = [];
  for ($i = 1; $i <= 12; $i++) {
    $cols[$i] = $i;
  }
  $form['#tree'] = TRUE;
  foreach ($breakpoints as $break => $data) {
    $form[$break] = [
      '#title' => $data['bs_label'],
      '#type' => 'details',
      '#open' => FALSE,
    ];
    $form[$break]['label'] = [
      '#title' => $this
        ->t('Label'),
      '#type' => 'textfield',
      '#default_value' => $config[$break]['label'],
      '#required' => TRUE,
    ];
    $form[$break]['prefix'] = [
      '#title' => $this
        ->t('Prefix'),
      '#type' => 'textfield',
      '#default_value' => $data['prefix'],
      '#disabled' => TRUE,
      '#required' => TRUE,
    ];
    $form[$break]['columns'] = [
      '#title' => $this
        ->t('Available Column Layouts'),
      '#type' => 'details',
      '#description' => $this
        ->t('In the format of key|value, where the key is the attribute to add to each column, separated by a comma. Special tags are "auto" and "equal"'),
    ];
    $options = [
      'equal' => $this
        ->t('Equal'),
      'auto' => $this
        ->t('Auto'),
    ] + $cols;

    // For now this is static to 12.
    foreach ($cols as $col) {
      $form[$break]['columns'][$col]['layouts'] = [
        '#title' => $this
          ->t('@num Column Layout', [
          '@num' => $col,
        ]),
        '#type' => 'details',
        '#prefix' => '<div id="fieldset-wrapper-' . $break . '-' . $col . '">',
        '#suffix' => '</div>',
        '#open' => FALSE,
      ];
      $num_layouts = $form_state
        ->get('num_ ' . $break . '_' . $col);
      if ($num_layouts === NULL) {
        $num = count($config[$break]['columns'][$col]['layouts']);
        $form_state
          ->set('num_ ' . $break . '_' . $col, $num);
        $num_layouts = $num;
      }
      for ($i = 0; $i < $num_layouts; $i++) {
        $form[$break]['columns'][$col]['layouts']['option_' . $i]['label'] = [
          '#title' => $this
            ->t('Label'),
          '#type' => 'textfield',
          '#required' => TRUE,
          '#default_value' => $config[$break]['columns'][$col]['layouts']['option_' . $i]['label'] ?? $this
            ->t('Equal Width'),
        ];
        $form[$break]['columns'][$col]['layouts']['option_' . $i]['settings'] = [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'container-inline',
            ],
          ],
        ];
        for ($j = 1; $j <= $col; $j++) {
          $form[$break]['columns'][$col]['layouts']['option_' . $i]['settings']['col-' . $j] = [
            '#title' => $j,
            '#type' => 'select',
            '#options' => $options,
            '#default_value' => $config[$break]['columns'][$col]['layouts']['option_' . $i]['settings']['col-' . $j],
          ];
        }

        // If there is more than one name, add the remove button.
        if ($num_layouts > 1) {
          $form[$break]['columns'][$col]['layouts']['option_' . $i]['settings']['remove'] = [
            '#type' => 'submit',
            '#value' => $this
              ->t('Remove'),
            '#submit' => [
              '::removeCallback',
            ],
            '#ajax' => [
              'callback' => '::ajaxCallback',
              'wrapper' => 'fieldset-wrapper-' . $break . '-' . $col,
            ],
            '#name' => 'remove-' . $break . '-' . $col,
          ];
        }
      }
      $form[$break]['columns'][$col]['layouts']['actions'] = [
        '#type' => 'actions',
      ];
      $form[$break]['columns'][$col]['layouts']['add_name'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Add More'),
        '#submit' => [
          '::addOne',
        ],
        '#ajax' => [
          'callback' => '::ajaxCallback',
          'wrapper' => 'fieldset-wrapper-' . $break . '-' . $col,
        ],
        '#name' => 'add-' . $break . '-' . $col,
      ];
    }
  }
  return parent::buildForm($form, $form_state);
}