You are here

protected function ConditionalFieldForm::buildTable in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/Form/ConditionalFieldForm.php \Drupal\conditional_fields\Form\ConditionalFieldForm::buildTable()

Builds table with conditional fields.

1 call to ConditionalFieldForm::buildTable()
ConditionalFieldForm::buildForm in src/Form/ConditionalFieldForm.php
Form constructor.

File

src/Form/ConditionalFieldForm.php, line 275

Class

ConditionalFieldForm
Class ConditionalFieldForm.

Namespace

Drupal\conditional_fields\Form

Code

protected function buildTable() {
  $table = [
    '#type' => 'table',
    '#entity_type' => $this->entity_type,
    '#bundle_name' => $this->bundle_name,
    '#header' => [
      $this
        ->t('Target field'),
      $this
        ->t('Controlled by'),
      [
        'data' => $this
          ->t('Description'),
        'colspan' => 2,
      ],
      [
        'data' => $this
          ->t('Operations'),
        'colspan' => 2,
      ],
    ],
  ];
  $fields = $this
    ->getFieldsList();

  /* Existing conditions. */

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_entity */
  $form_display_entity = $this->entityTypeManager
    ->getStorage('entity_form_display')
    ->load("{$this->entity_type}.{$this->bundle_name}.default");
  if (empty($form_display_entity) && $entity_type == 'taxonomy_term') {
    $form_display_entity = $this->entityTypeManager
      ->getStorage('entity_form_display')
      ->create([
      'targetEntityType' => 'taxonomy_term',
      'bundle' => $bundle_name,
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }
  if (!$form_display_entity) {
    $this->form['conditional_fields_wrapper']['table'] = $table;
    return;
  }
  foreach ($fields as $field_name => $label) {
    $field = $form_display_entity
      ->getComponent($field_name);
    if (empty($field['third_party_settings']['conditional_fields'])) {
      continue;
    }

    // Create row for existing field's conditions.
    foreach ($field['third_party_settings']['conditional_fields'] as $uuid => $condition) {
      $parameters = [
        'entity_type' => $condition['entity_type'],
        'bundle' => $condition['bundle'],
        'field_name' => $field_name,
        'uuid' => $uuid,
      ];
      $table[] = [
        'dependent' => [
          '#markup' => $field_name,
        ],
        'dependee' => [
          '#markup' => $condition['dependee'],
        ],
        'state' => [
          '#markup' => $condition['settings']['state'],
        ],
        'condition' => [
          '#markup' => $condition['settings']['condition'],
        ],
        'actions' => [
          '#type' => 'operations',
          '#links' => [
            'edit' => [
              'title' => $this
                ->t('Edit'),
              'url' => Url::fromRoute($this->editPath, $parameters),
            ],
            'delete' => [
              'title' => $this
                ->t('Delete'),
              'url' => Url::fromRoute($this->deletePath, $parameters),
            ],
          ],
        ],
      ];
    }
  }

  /* Row for creating new condition. */

  // Build list of states.
  $states = $this->list
    ->conditionalFieldsStates();

  // Build list of conditions.
  $conditions = [];
  foreach ($this->list
    ->conditionalFieldsConditions() as $condition => $label) {
    $label = (string) $label;
    $conditions[$condition] = $condition == 'value' ? $this
      ->t('has value...') : $this
      ->t('is @label', [
      '@label' => (string) $label,
    ]);
  }

  // Add new dependency row.
  $table['add_new_dependency'] = [
    'dependent' => [
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => $this
        ->t('Target field'),
      '#title_display' => 'invisible',
      '#description' => $this
        ->t('Target field'),
      '#options' => $fields,
      '#prefix' => '<div class="add-new-placeholder">' . $this
        ->t('Add new dependency') . '</div>',
      '#required' => TRUE,
      '#attributes' => [
        'class' => [
          'conditional-fields-selector',
        ],
        'style' => [
          'resize: both;',
        ],
      ],
    ],
    'dependee' => [
      '#type' => 'select',
      '#title' => $this
        ->t('Controlled by'),
      '#title_display' => 'invisible',
      '#description' => $this
        ->t('Control field'),
      '#options' => $fields,
      '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
      '#required' => TRUE,
      '#attributes' => [
        'class' => [
          'conditional-fields-selector',
        ],
      ],
    ],
    'state' => [
      '#type' => 'select',
      '#title' => $this
        ->t('State'),
      '#title_display' => 'invisible',
      '#options' => $states,
      '#default_value' => 'visible',
      '#prefix' => $this
        ->t('The target field is'),
    ],
    'condition' => [
      '#type' => 'select',
      '#title' => $this
        ->t('Condition'),
      '#title_display' => 'invisible',
      '#options' => $conditions,
      '#default_value' => 'value',
      '#prefix' => $this
        ->t('when the control field'),
    ],
    'actions' => [
      'submit' => [
        '#type' => 'submit',
        '#value' => $this
          ->t('Add dependency'),
      ],
    ],
  ];
  $table['#attached']['library'][] = 'conditional_fields/admin';
  $this->form['conditional_fields_wrapper']['table'] = $table;
}