You are here

public function FieldValidationRuleSetEditForm::form in Field Validation 8

Gets the actual form array to be built.

Overrides FieldValidationRuleSetFormBase::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/FieldValidationRuleSetEditForm.php, line 50

Class

FieldValidationRuleSetEditForm
Controller for blocktabs edit form.

Namespace

Drupal\field_validation\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $user_input = $form_state
    ->getUserInput();
  $form['#title'] = $this
    ->t('Edit field validation rule set %name', array(
    '%name' => $this->entity
      ->label(),
  ));
  $form['#tree'] = TRUE;

  //$form['#attached']['library'][] = 'field_validation/admin';

  // Build the list of existing field validation rule for this rule set.
  $form['rules'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Title'),
      $this
        ->t('Weight'),
      $this
        ->t('Plugin'),
      $this
        ->t('Field'),
      $this
        ->t('Operations'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'rule-order-weight',
      ],
    ],
    '#attributes' => [
      'id' => 'field-validation-rule-set-rules',
    ],
    '#empty' => $this
      ->t('There are currently no rules in this rule set. Add one by selecting an option below.'),
    // Render tabs below parent elements.
    '#weight' => 5,
  ];
  $field_validation_rules = $this->entity
    ->getFieldValidationRules();

  //drupal_set_message(var_export($field_validation_rules, true));
  foreach ($field_validation_rules as $field_validation_rule) {

    //drupal_set_message('abcd');
    $key = $field_validation_rule
      ->getUuid();
    $form['rules'][$key]['#attributes']['class'][] = 'draggable';
    $form['rules'][$key]['#weight'] = isset($user_input['rules']) ? $user_input['rules'][$key]['weight'] : NULL;
    $form['rules'][$key]['rule'] = [
      '#tree' => FALSE,
      'data' => [
        'label' => [
          '#plain_text' => $field_validation_rule
            ->label(),
        ],
      ],
    ];

    //$form['rules'][$key]['field_name'] = [

    //  '#markup' => $field_validation_rule->getFieldName(),

    //];

    //$summary = $field_validation_rule->getSummary();

    //if (!empty($summary)) {

    //  $summary['#prefix'] = ' ';
    //  $form['rules'][$key]['rule']['data']['summary'] = $summary;

    //}
    $form['rules'][$key]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $field_validation_rule
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $field_validation_rule
        ->getWeight(),
      '#attributes' => [
        'class' => [
          'rule-order-weight',
        ],
      ],
    ];
    $form['rules'][$key]['id'] = [
      '#markup' => $field_validation_rule
        ->getPluginId(),
    ];
    $form['rules'][$key]['field'] = [
      '#type' => 'markup',
      '#markup' => $field_validation_rule
        ->getFieldName(),
    ];
    $links = [];
    $is_configurable = $field_validation_rule instanceof ConfigurableFieldValidationRuleInterface;
    if ($is_configurable) {
      $links['edit'] = [
        'title' => $this
          ->t('Edit'),
        'url' => Url::fromRoute('field_validation.field_validation_rule_edit_form', [
          'field_validation_rule_set' => $this->entity
            ->id(),
          'field_validation_rule' => $key,
        ]),
      ];
    }
    $links['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('field_validation.field_validation_rule_delete', [
        'field_validation_rule_set' => $this->entity
          ->id(),
        'field_validation_rule' => $key,
      ]),
    ];
    $form['rules'][$key]['operations'] = [
      '#type' => 'operations',
      '#links' => $links,
    ];
  }

  // Build the new field_validation_rule addition form and add it to the field_validation_rule list.
  $new_field_validation_rule_options = [];
  $field_validation_rules = $this->fieldValidationRuleManager
    ->getDefinitions();

  //drupal_set_message(var_export($field_validation_rules, true));
  uasort($field_validation_rules, function ($a, $b) {
    return strcasecmp($a['id'], $b['id']);
  });
  foreach ($field_validation_rules as $field_validation_rule => $definition) {
    $new_field_validation_rule_options[$field_validation_rule] = $definition['label'];
  }
  $form['rules']['new'] = [
    '#tree' => FALSE,
    '#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
    '#attributes' => [
      'class' => [
        'draggable',
      ],
    ],
  ];
  $form['rules']['new']['rule'] = [
    'data' => [
      'new' => [
        '#type' => 'select',
        '#title' => $this
          ->t('Rule'),
        '#title_display' => 'invisible',
        '#options' => $new_field_validation_rule_options,
        '#empty_option' => $this
          ->t('Select a new rule'),
      ],
      [
        'add' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Add'),
          '#validate' => [
            '::fieldValidationRuleValidate',
          ],
          '#submit' => [
            '::submitForm',
            '::fieldValidationRuleSave',
          ],
        ],
      ],
    ],
    '#prefix' => '<div class="field-validation-rule-new">',
    '#suffix' => '</div>',
  ];
  $form['rules']['new']['weight'] = [
    '#type' => 'weight',
    '#title' => $this
      ->t('Weight for new rule'),
    '#title_display' => 'invisible',
    '#default_value' => count($this->entity
      ->getFieldValidationRules()) + 1,
    '#attributes' => [
      'class' => [
        'rule-order-weight',
      ],
    ],
  ];
  $form['rules']['new']['operations'] = [
    'data' => [],
  ];
  return parent::form($form, $form_state);
}