You are here

public function FieldValidationRuleFormBase::buildForm in Field Validation 8

Parameters

\Drupal\field_validation\FieldValidationRuleSetInterface $field_validation_rule_set: The field_validation_rule_set.

string $field_validation_rule: The field_validation_rule ID.

Return value

array The form structure.

Throws

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

Overrides FormInterface::buildForm

2 calls to FieldValidationRuleFormBase::buildForm()
FieldValidationRuleAddForm::buildForm in src/Form/FieldValidationRuleAddForm.php
Form constructor.
FieldValidationRuleEditForm::buildForm in src/Form/FieldValidationRuleEditForm.php
Form constructor.
2 methods override FieldValidationRuleFormBase::buildForm()
FieldValidationRuleAddForm::buildForm in src/Form/FieldValidationRuleAddForm.php
Form constructor.
FieldValidationRuleEditForm::buildForm in src/Form/FieldValidationRuleEditForm.php
Form constructor.

File

src/Form/FieldValidationRuleFormBase.php, line 53

Class

FieldValidationRuleFormBase
Provides a base form for FieldValidationRule.

Namespace

Drupal\field_validation\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, FieldValidationRuleSetInterface $field_validation_rule_set = NULL, $field_validation_rule = NULL, $field_name = '') {
  $this->fieldValidationRuleSet = $field_validation_rule_set;
  try {
    $this->fieldValidationRule = $this
      ->prepareFieldValidationRule($field_validation_rule);
  } catch (PluginNotFoundException $e) {
    throw new NotFoundHttpException("Invalid field_validation_rule id: '{$field_validation_rule}'.");
  }
  $request = $this
    ->getRequest();
  if (!$this->fieldValidationRule instanceof ConfigurableFieldValidationRuleInterface) {
    throw new NotFoundHttpException();
  }

  //$form['#attached']['library'][] = 'field_validation/admin';
  $form['uuid'] = [
    '#type' => 'hidden',
    '#value' => $this->fieldValidationRule
      ->getUuid(),
  ];
  $form['id'] = [
    '#type' => 'hidden',
    '#value' => $this->fieldValidationRule
      ->getPluginId(),
  ];
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Field Validation Rule title'),
    '#default_value' => $this->fieldValidationRule
      ->getTitle(),
    '#required' => TRUE,
  ];
  $entity_type_id = $this->fieldValidationRuleSet
    ->getAttachedEntityType();
  $bundle = $this->fieldValidationRuleSet
    ->getAttachedBundle();

  //$field_options = array();
  $field_options = array(
    '' => $this
      ->t('- Select -'),
  );
  $baseFieldDefinitions = \Drupal::service('entity_field.manager')
    ->getBaseFieldDefinitions($entity_type_id);
  foreach ($baseFieldDefinitions as $base_field_name => $base_field_definition) {
    $field_options[$base_field_name] = $base_field_definition
      ->getLabel();
  }
  $fieldDefinitions = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions($entity_type_id, $bundle);
  foreach ($fieldDefinitions as $fieldname => $field_definition) {
    if (!empty($field_definition
      ->getTargetBundle())) {
      $field_options[$fieldname] = $field_definition
        ->getLabel();

      //if($field_name == 'field_test'){

      // $field_all = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', $field_name);

      //$field_all = $field_definition->getPropertyDefinitions();

      //$field_storage_definitions = \Drupal::entityManager()->getFieldStorageDefinitions('node');

      // $field_all = \Drupal\field\Entity\FieldStorageConfig::loadByName('node', $field_name);
      // $schema =$field_all->getSchema();

      //$field_storage = $field_storage_definitions[$field_name]->getPropertyDefinitions();

      //$field_storage = $field_storage_definitions[$field_name]->getPropertyDefinitions();

      // drupal_set_message(var_export($schema));

      //}
    }
  }
  $default_field_name = $this->fieldValidationRule
    ->getFieldName();
  if (!empty($field_name)) {
    $default_field_name = $field_name;
  }
  $user_input = $form_state
    ->getUserInput();
  $default_field_name = isset($user_input['field_name']) ? $user_input['field_name'] : $default_field_name;
  $form['field_name'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field name'),
    '#options' => $field_options,
    '#default_value' => $default_field_name,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $this,
        'updateColumn',
      ],
      'wrapper' => 'edit-field-name-wrapper',
      'event' => 'change',
    ],
  ];

  //$default_field_name = $form_state->getValue('field_name', $field_name);
  $default_column = $this->fieldValidationRule
    ->getColumn();

  //$user_input = $form_state->getUserInput();
  $default_column = isset($user_input['column']) ? $user_input['column'] : $default_column;
  $column_options = $this
    ->findColumn($default_field_name);
  if (!in_array($default_column, $column_options)) {
    $default_column = "";
  }

  //$default_column = $form_state->getValue('column', $default_column);

  //if()
  $form['column'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Column of field'),
    '#options' => $column_options,
    '#default_value' => $default_column,
    '#required' => TRUE,
    '#prefix' => '<div id="edit-field-name-wrapper">',
    '#suffix' => '</div>',
    '#validated' => TRUE,
  ];
  $form['data'] = $this->fieldValidationRule
    ->buildConfigurationForm([], $form_state);
  $form['error_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Error message'),
    '#default_value' => $this->fieldValidationRule
      ->getErrorMessage(),
    '#required' => TRUE,
    '#maxlength' => 255,
  ];
  $form['data']['#tree'] = TRUE;

  //drupal_set_message('term_id:' . var_export($form['data']));

  // Check the URL for a weight, then the fieldValidationRule, otherwise use default.
  $form['weight'] = [
    '#type' => 'hidden',
    '#value' => $request->query
      ->has('weight') ? (int) $request->query
      ->get('weight') : $this->fieldValidationRule
      ->getWeight(),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#button_type' => 'primary',
  ];
  $form['actions']['cancel'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Cancel'),
    '#url' => $this->fieldValidationRuleSet
      ->toUrl('edit-form'),
    '#attributes' => [
      'class' => [
        'button',
      ],
    ],
  ];
  return $form;
}