protected function JsonapiResourceConfigForm::buildOverridesForm in JSON:API Extras 8.3
Same name and namespace in other branches
- 8 src/Form/JsonapiResourceConfigForm.php \Drupal\jsonapi_extras\Form\JsonapiResourceConfigForm::buildOverridesForm()
- 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.
Throws
\Drupal\Component\Plugin\Exception\PluginNotFoundException
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 235
Class
- JsonapiResourceConfigForm
- Base form for jsonapi_resource_config.
Namespace
Drupal\jsonapi_extras\FormCode
protected function buildOverridesForm(ResourceType $resource_type, JsonapiResourceConfig $entity) {
$entity_type_id = $resource_type
->getEntityTypeId();
/** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$bundle = $resource_type
->getBundle();
$field_names = $this
->getAllFieldNames($entity_type, $bundle);
$overrides_form['overrides']['entity'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Resource'),
'#description' => $this
->t('Override configuration for the resource entity.'),
'#open' => !$entity
->get('resourceType') || !$entity
->get('path'),
'#weight' => 0,
];
$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,
'#weight' => 1,
];
$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',
'#theme' => 'expandable_rows_table',
'#header' => [
'disabled' => $this
->t('Disabled'),
'fieldName' => $this
->t('Field name'),
'publicName' => $this
->t('Alias'),
'advancedOptions' => '',
],
'#empty' => $this
->t('No fields available.'),
'#states' => [
'visible' => [
':input[name="disabled"]' => [
'checked' => FALSE,
],
],
],
'#attached' => [
'library' => [
'jsonapi_extras/expandable_rows_table',
],
],
];
foreach ($field_names as $field_name) {
try {
$overrides = $this
->buildOverridesField($field_name, $entity);
} catch (PluginException $exception) {
// Log exception and continue.
watchdog_exception('jsonapi_extras', $exception);
continue;
}
NestedArray::setValue($overrides_form, [
'overrides',
'fields',
'resourceFields',
$field_name,
], $overrides);
}
return $overrides_form;
}