You are here

public function QuickNodeCloneEntitySettingsForm::buildForm in Quick Node Clone 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

1 call to QuickNodeCloneEntitySettingsForm::buildForm()
QuickNodeCloneNodeSettingsForm::buildForm in src/Form/QuickNodeCloneNodeSettingsForm.php
Form constructor.
1 method overrides QuickNodeCloneEntitySettingsForm::buildForm()
QuickNodeCloneNodeSettingsForm::buildForm in src/Form/QuickNodeCloneNodeSettingsForm.php
Form constructor.

File

src/Form/QuickNodeCloneEntitySettingsForm.php, line 101

Class

QuickNodeCloneEntitySettingsForm
Abstract class to configure how entities are cloned.

Namespace

Drupal\quick_node_clone\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['exclude'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Exclusion list'),
  ];
  $form['exclude']['description'] = [
    '#markup' => $this
      ->t('You can select fields that you do not want to be included when the node is cloned.'),
  ];
  $config_name = 'exclude.' . $this
    ->getEntityTypeId();
  if (!is_null($this
    ->getSettings($config_name))) {
    $value = $this
      ->getSettings($config_name);
    if (empty($form_state
      ->getValue('bundle_names'))) {
      $form_state
        ->setValue('bundle_names', $value);
    }
  }
  $bundle_names = [];
  foreach ($this
    ->getEntityBundles() as $bundle => $item) {
    $bundle_names[$bundle] = $item['label'];
  }
  $form['exclude']['bundle_names'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Entity Types'),
    '#options' => $bundle_names,
    '#default_value' => array_keys($form_state
      ->getValue('bundle_names') ?: []),
    '#description' => $this
      ->t('Select entity types above and you will see a list of fields that can be excluded.'),
    '#ajax' => [
      'callback' => 'Drupal\\quick_node_clone\\Form\\QuickNodeCloneEntitySettingsForm::fieldsCallback',
      'wrapper' => 'fields-list-' . $this
        ->getEntityTypeId(),
      'method' => 'replace',
    ],
  ];
  $form['exclude']['fields'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Fields'),
    '#description' => $this
      ->getDescription($form_state),
    '#prefix' => '<div id="fields-list-' . $this
      ->getEntityTypeId() . '">',
    '#suffix' => '</div>',
  ];
  if ($selected_bundles = $this
    ->getSelectedBundles($form_state)) {
    $selected_bundles = $this
      ->getSelectedBundles($form_state);
    foreach ($bundle_names as $bundle_name => $bundle_label) {
      if (!empty($selected_bundles[$bundle_name])) {
        $options = [];
        $field_definitions = $this->entityFieldManager
          ->getFieldDefinitions($this
          ->getEntityTypeId(), $bundle_name);
        foreach ($field_definitions as $field) {
          if ($field instanceof FieldConfig) {
            $options[$field
              ->getName()] = $field
              ->getLabel();
          }
        }
        $form['exclude']['fields']['bundle_' . $bundle_name] = [
          '#type' => 'details',
          '#title' => $bundle_name,
          '#open' => TRUE,
        ];
        $form['exclude']['fields']['bundle_' . $bundle_name][$bundle_name] = [
          '#type' => 'checkboxes',
          '#title' => $this
            ->t('Fields for @bundle_name', [
            '@bundle_name' => $bundle_name,
          ]),
          '#default_value' => $this
            ->getDefaultFields($bundle_name),
          '#options' => $options,
        ];
      }
    }
  }
  return parent::buildForm($form, $form_state);
}