You are here

protected function JsonapiResourceConfigForm::buildOverridesField in JSON:API Extras 8

Same name and namespace in other branches
  1. 8.3 src/Form/JsonapiResourceConfigForm.php \Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm::buildOverridesField()
  2. 8.2 src/Form/JsonapiResourceConfigForm.php \Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm::buildOverridesField()

Builds the part of the form that overrides the field.

Parameters

string $field_name: The field name of the field being overridden.

\Drupal\jsonapi_extras\Entity\JsonapiResourceConfig $entity: The config entity backed by this form.

Return value

array The partial form.

1 call to JsonapiResourceConfigForm::buildOverridesField()
JsonapiResourceConfigForm::buildOverridesForm in src/Form/JsonapiResourceConfigForm.php
Builds the part of the form that contains the overrides.

File

src/Form/JsonapiResourceConfigForm.php, line 360

Class

JsonapiResourceConfigForm
Base form for jsonapi_resource_config.

Namespace

Drupal\jsonapi_extras\Form

Code

protected function buildOverridesField($field_name, JsonapiResourceConfig $entity) {
  $resource_fields = array_filter($entity
    ->get('resourceFields'), function (array $resource_field) use ($field_name) {
    return $resource_field['fieldName'] == $field_name;
  });
  $resource_field = array_shift($resource_fields);
  $overrides_form = [];
  $overrides_form['disabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Disabled'),
    '#title_display' => 'invisible',
    '#default_value' => $resource_field['disabled'],
  ];
  $overrides_form['fieldName'] = [
    '#type' => 'hidden',
    '#value' => $field_name,
    '#prefix' => $field_name,
  ];
  $overrides_form['publicName'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Override Public Name'),
    '#title_display' => 'hidden',
    '#default_value' => empty($resource_field['publicName']) ? $field_name : $resource_field['publicName'],
    '#states' => [
      'visible' => [
        ':input[name="resourceFields[' . $field_name . '][disabled]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];

  // Build the select field for the list of enhancers.
  $overrides_form['enhancer'] = [
    '#type' => 'fieldgroup',
    '#states' => [
      'visible' => [
        ':input[name="resourceFields[' . $field_name . '][disabled]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $options = array_reduce($this->enhancerManager
    ->getDefinitions(), function (array $carry, array $definition) {
    $carry[$definition['id']] = $definition['label'];
    return $carry;
  }, [
    '' => $this
      ->t('- Select -'),
  ]);
  $id = empty($resource_field['enhancer']['id']) ? '' : $resource_field['enhancer']['id'];
  $overrides_form['enhancer']['id'] = [
    '#type' => 'select',
    '#options' => $options,
    '#ajax' => [
      'callback' => '::getEnhancerSettings',
      'wrapper' => $field_name . '-settings-wrapper',
    ],
    '#default_value' => $id,
  ];
  $overrides_form['enhancer']['settings'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => $field_name . '-settings-wrapper',
    ],
  ];
  if (!empty($resource_field['enhancer']['id'])) {

    /** @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface $enhancer */
    $enhancer = $this->enhancerManager
      ->createInstance($resource_field['enhancer']['id'], []);
    $overrides_form['enhancer']['settings'] += $enhancer
      ->getSettingsForm($resource_field);
  }
  return $overrides_form;
}