You are here

public function SiteSettingReplicateForm::buildForm in Site Settings and Labels 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/SiteSettingReplicateForm.php, line 63

Class

SiteSettingReplicateForm
Class SiteSettingReplicateForm.

Namespace

Drupal\site_settings\Form

Code

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

  // Optional dependencies check here as we don't want to force the use of
  // these modules for the core functionality of this module.
  $has_replicate = $this->moduleHandler
    ->moduleExists('replicate');
  $has_field_tools = $this->moduleHandler
    ->moduleExists('field_tools');
  if (!$has_replicate && !$has_field_tools) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Please install the Replicate and Field Tools modules to use this feature.'), 'warning');
  }
  elseif (!$has_replicate) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Please install the Replicate module to use this feature.'), 'warning');
  }
  elseif (!$has_field_tools) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Please install the Field Tools module to use this feature.'), 'warning');
  }
  elseif ($site_setting_entity_type = $this->entityTypeManager
    ->getStorage('site_setting_entity_type')
    ->load($setting)) {

    /** @var \Drupal\site_settings\Entity\SiteSettingEntityType $site_setting_entity_type */

    // The form.
    $form['setting'] = [
      '#type' => 'hidden',
      '#value' => $setting,
    ];
    $form['description'] = [
      '#markup' => '<h2>' . $this
        ->t('Replicating from setting: @setting', [
        '@setting' => $setting,
      ]) . '</h2>',
    ];

    // State that the form needs to allow for a hierarchy.
    $form['#tree'] = TRUE;

    // Initial number of names.
    if (!$form_state
      ->get('num_to_generate')) {
      $form_state
        ->set('num_to_generate', 1);
    }

    // Container for our repeating fields.
    $form['new_settings'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Machine name'),
        $this
          ->t('Label'),
        $this
          ->t('Fieldset'),
      ],
    ];

    // Add our names fields.
    for ($x = 0; $x < $form_state
      ->get('num_to_generate'); $x++) {
      $form['new_settings'][$x]['machine_name'] = [
        '#type' => 'machine_name',
        '#title' => '',
        '#description' => '',
        '#size' => 30,
        '#required' => FALSE,
        '#machine_name' => [
          'exists' => [
            $this,
            'machineNameExists',
          ],
        ],
      ];
      $form['new_settings'][$x]['label'] = [
        '#type' => 'textfield',
        '#title' => '',
        '#description' => '',
        '#size' => 20,
      ];
      $form['new_settings'][$x]['fieldset'] = [
        '#type' => 'textfield',
        '#title' => '',
        '#description' => '',
        '#size' => 20,
        '#default_value' => $site_setting_entity_type->fieldset,
      ];
    }
    $form['button_container'] = [
      '#type' => 'container',
    ];
    $form['button_container']['add_setting'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add another setting'),
      '#limit_validation_errors' => [],
    ];
    $form['button_container']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Generate settings'),
    ];
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Unable to load setting.'), 'warning');
  }
  return $form;
}