You are here

public function GeneralSettingsForm::buildForm in Diff 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 ConfigFormBase::buildForm

File

src/Form/GeneralSettingsForm.php, line 67

Class

GeneralSettingsForm
Configure global diff settings.

Namespace

Drupal\diff\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $field_type = NULL) {
  $config = $this
    ->config('diff.settings');
  $form['radio_behavior'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Diff radio behavior'),
    '#default_value' => $config
      ->get('general_settings' . '.' . 'radio_behavior'),
    '#options' => array(
      'simple' => $this
        ->t('Simple exclusion'),
      'linear' => $this
        ->t('Linear restrictions'),
    ),
    '#empty_option' => $this
      ->t('- None -'),
    '#description' => $this
      ->t('<em>Simple exclusion</em> means that users will not be able to select the same revision, <em>Linear restrictions</em> means that users can only select older or newer revisions of the current selections.'),
  );
  $layout_plugins = $this->diffLayoutManager
    ->getDefinitions();
  $weight = count($layout_plugins) + 1;
  $layout_plugins_order = [];
  foreach ($layout_plugins as $id => $layout_plugin) {
    $layout_plugin_settings = $config
      ->get('general_settings.layout_plugins')[$id];
    $layout_plugins_order[$id] = [
      'label' => $layout_plugin['label'],
      'description' => $layout_plugin['description'] ?: '',
      'enabled' => $layout_plugin_settings['enabled'],
      'weight' => isset($layout_plugin_settings['weight']) ? $layout_plugin_settings['weight'] : $weight,
    ];
    $weight++;
  }
  $form['layout_plugins'] = [
    '#type' => 'table',
    '#header' => [
      t('Layout'),
      t('Description'),
      t('Weight'),
    ],
    '#empty' => t('There are no items yet. Add an item.'),
    '#suffix' => '<div class="description">' . $this
      ->t('The layout plugins that are enabled to display the revision comparison.') . '</div>',
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'diff-layout-plugins-order-weight',
      ],
    ],
  ];
  uasort($layout_plugins_order, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
  foreach ($layout_plugins_order as $id => $layout_plugin) {
    $form['layout_plugins'][$id] = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
      'enabled' => [
        '#type' => 'checkbox',
        '#title' => $layout_plugin['label'],
        '#title_display' => 'after',
        '#default_value' => $layout_plugin['enabled'],
      ],
      'description' => [
        '#type' => 'markup',
        '#markup' => Xss::filter($layout_plugin['description']),
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => t('Weight for @title', [
          '@title' => $layout_plugin['label'],
        ]),
        '#title_display' => 'invisible',
        '#delta' => 50,
        '#default_value' => (int) $layout_plugin['weight'],
        '#array_parents' => [
          'settings',
          'sites',
          $id,
        ],
        '#attributes' => [
          'class' => [
            'diff-layout-plugins-order-weight',
          ],
        ],
      ],
    ];
  }
  $form['field_based_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Field based layout settings'),
    '#open' => TRUE,
    '#states' => [
      'visible' => [
        [
          ':input[name="layout_plugins[split_fields][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
        [
          ':input[name="layout_plugins[unified_fields][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ],
  ];
  $context_lines = array(
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
  );
  $options = array_combine($context_lines, $context_lines);
  $form['field_based_settings']['context_lines_leading'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Leading'),
    '#description' => $this
      ->t('This governs the number of unchanged <em>leading context "lines"</em> to preserve.'),
    '#default_value' => $config
      ->get('general_settings' . '.' . 'context_lines_leading'),
    '#options' => $options,
  );
  $form['field_based_settings']['context_lines_trailing'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Trailing'),
    '#description' => $this
      ->t('This governs the number of unchanged <em>trailing context "lines"</em> to preserve.'),
    '#default_value' => $config
      ->get('general_settings' . '.' . 'context_lines_trailing'),
    '#options' => $options,
  );

  // Check if Visual inline layout is installed.
  if ($this->diffLayoutManager
    ->hasDefinition('visual_inline')) {
    $form['visual_inline_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Visual layout settings'),
      '#open' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="layout_plugins[visual_inline][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];

    // Get the theme data to display the related theme name.
    $default_theme_name = $this
      ->config('system.theme')
      ->get('default');
    $admin_theme_name = $this
      ->config('system.theme')
      ->get('admin');
    $form['visual_inline_settings']['visual_inline_theme'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Theme'),
      '#options' => [
        'default' => $this
          ->t('Default'),
        'admin' => $this
          ->t('Admin'),
      ],
      '#description' => $this
        ->t('Use Default to display the comparison as %default theme, or Admin as %admin theme.', [
        '%default' => $default_theme_name,
        '%admin' => $admin_theme_name,
      ]),
      '#default_value' => $config
        ->get('general_settings')['visual_inline_theme'],
    ];
  }
  return parent::buildForm($form, $form_state);
}