You are here

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

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

Builds the part of the form that contains the overrides.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The resource type being overridden.

\Drupal\jsonapi_extras\Entity\JsonapiResourceConfig $entity: The configuration entity backing this form.

Return value

array The partial form.

1 call to JsonapiResourceConfigForm::buildOverridesForm()
JsonapiResourceConfigForm::form in src/Form/JsonapiResourceConfigForm.php
Gets the actual form array to be built.

File

src/Form/JsonapiResourceConfigForm.php, line 225

Class

JsonapiResourceConfigForm
Base form for jsonapi_resource_config.

Namespace

Drupal\jsonapi_extras\Form

Code

protected function buildOverridesForm(ResourceType $resource_type, JsonapiResourceConfig $entity) {
  $entity_type_id = $resource_type
    ->getEntityTypeId();
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type_id);
  $bundle = $resource_type
    ->getBundle();
  if ($entity_type instanceof ContentEntityTypeInterface) {
    $field_names = array_map(function (FieldDefinitionInterface $field_definition) {
      return $field_definition
        ->getName();
    }, $this->fieldManager
      ->getFieldDefinitions($entity_type_id, $bundle));
  }
  else {
    $field_names = array_keys($entity_type
      ->getPropertiesToExport());
    array_unshift($field_names, $entity_type
      ->getKey('id'));
  }
  $overrides_form['overrides']['entity'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Entity'),
    '#description' => $this
      ->t('Override configuration for the resource entity.'),
    '#open' => !$entity
      ->get('resourceType') || !$entity
      ->get('path'),
  ];
  $overrides_form['overrides']['entity']['disabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Disabled'),
    '#description' => $this
      ->t('Check this if you want to disable this resource. Disabling a resource can have unexpected results when following relationships belonging to that resource.'),
    '#default_value' => $entity
      ->get('disabled'),
  ];
  $resource_type_name = $entity
    ->get('resourceType');
  if (!$resource_type_name) {
    $resource_type_name = sprintf('%s--%s', $entity_type_id, $bundle);
  }
  $overrides_form['overrides']['entity']['resourceType'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Resource Type'),
    '#description' => $this
      ->t('Overrides the type of the resource. Example: Change "node--article" to "articles".'),
    '#default_value' => $resource_type_name,
    '#states' => [
      'visible' => [
        ':input[name="disabled"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $path = $entity
    ->get('path');
  if (!$path) {
    $path = sprintf('%s/%s', $entity_type_id, $bundle);
  }
  $prefix = $this->config
    ->get('path_prefix');
  $overrides_form['overrides']['entity']['path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Resource Path'),
    '#field_prefix' => sprintf('/%s/', $prefix),
    '#description' => $this
      ->t('Overrides the path of the resource. Example: Use "articles" to change "/@prefix/node/article" to "/@prefix/articles".', [
      '@prefix' => $prefix,
    ]),
    '#default_value' => $path,
    '#required' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="disabled"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $overrides_form['overrides']['fields'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Fields'),
    '#open' => TRUE,
  ];
  $markup = '';
  $markup .= '<dl>';
  $markup .= '<dt>' . $this
    ->t('Disabled') . '</dt>';
  $markup .= '<dd>' . $this
    ->t('Check this if you want to disable this field completely. Disabling required fields will cause problems when writing to the resource.') . '</dd>';
  $markup .= '<dt>' . $this
    ->t('Alias') . '</dt>';
  $markup .= '<dd>' . $this
    ->t('Overrides the field name with a custom name. Example: Change "field_tags" to "tags".') . '</dd>';
  $markup .= '<dt>' . $this
    ->t('Enhancer') . '</dt>';
  $markup .= '<dd>' . $this
    ->t('Select an enhancer to manipulate the public output coming in and out.') . '</dd>';
  $markup .= '</dl>';
  $overrides_form['overrides']['fields']['info'] = [
    '#markup' => $markup,
  ];
  $overrides_form['overrides']['fields']['resourceFields'] = [
    '#type' => 'table',
    '#header' => [
      'disabled' => $this
        ->t('Disabled'),
      'fieldName' => $this
        ->t('Field name'),
      'publicName' => $this
        ->t('Alias'),
      'enhancer' => $this
        ->t('Enhancer'),
    ],
    '#empty' => $this
      ->t('No fields available.'),
    '#states' => [
      'visible' => [
        ':input[name="disabled"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  foreach ($field_names as $field_name) {
    $overrides_form['overrides']['fields']['resourceFields'][$field_name] = $this
      ->buildOverridesField($field_name, $entity);
  }
  return $overrides_form;
}