feat(Misc): Create config entity from UI

However, the non-bundle entity types are not working yet.
parent c865b7e8
......@@ -10,3 +10,6 @@ This module adds the following features:
- Allows you to customize the resource type to other than
`${entityTypeId}--${bundle}`.
- Lets you remove fields from the JSON API output.
TODO:
* Auto calculate the dependency of the provider of the entity type and bundles in the configuration entity.
......@@ -4,7 +4,7 @@ jsonapi_extras.resource_config.*:
mapping:
id:
type: string
label: 'Original Resource plugin ID.'
label: 'Original Resource ID.'
path:
type: string
label: 'Path'
......@@ -22,15 +22,12 @@ jsonapi_extras.resource_config.*:
jsonapi_extras.resource_field:
type: mapping
mapping:
fieldName:
type: string
label: 'Entity field name'
publicName:
type: string
label: 'Public attribute name'
id:
type: string
label: 'Resource field type'
class:
type: string
label: 'Wrapper class'
postNormalizationCallbacks:
type: sequence
label: 'Post-normalization callbacks'
......
......@@ -7,10 +7,14 @@
namespace Drupal\jsonapi_extras\Form;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\jsonapi\ResourceType\ResourceTypeRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -31,15 +35,21 @@ class ResourceConfigForm extends EntityForm {
*/
protected $resourceTypeRepository;
/**
* @var \Drupal\Core\Entity\EntityFieldManager
*/
protected $fieldManager;
/**
* EntityBundlePicker constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
*/
public function __construct(EntityTypeBundleInfoInterface $bundle_info, ResourceTypeRepository $resource_type_repository) {
public function __construct(EntityTypeBundleInfoInterface $bundle_info, ResourceTypeRepository $resource_type_repository, EntityFieldManager $field_manager) {
$this->bundleInfo = $bundle_info;
$this->resourceTypeRepository = $resource_type_repository;
$this->fieldManager = $field_manager;
}
......@@ -49,7 +59,8 @@ class ResourceConfigForm extends EntityForm {
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.bundle.info'),
$container->get('jsonapi.resource_type.repository')
$container->get('jsonapi.resource_type.repository'),
$container->get('entity_field.manager')
);
}
......@@ -62,11 +73,10 @@ class ResourceConfigForm extends EntityForm {
$entity_types = $this->entityTypeManager->getDefinitions();
$entity_type_options = array_reduce($entity_types, function ($carry, EntityTypeInterface $entity_type) {
$carry[$entity_type->id()] = $entity_type->getLabel();
return $carry;
}, []);
$form['entity_type_id'] = [
$form['_entity_type_id'] = [
'#title' => $this->t('Entity Type'),
'#type' => 'select',
'#options' => $entity_type_options,
......@@ -95,11 +105,7 @@ class ResourceConfigForm extends EntityForm {
'#type' => 'container',
'#attributes' => ['id' => 'bundle-wrapper'],
];
$form['fields_wrapper'] = [
'#type' => 'container',
'#attributes' => ['id' => 'fields-wrapper'],
];
if (!$entity_type_id = $form_state->getValue('entity_type_id')) {
if (!$entity_type_id = $form_state->getValue('_entity_type_id')) {
return $form;
}
$has_bundles = (bool) $this->entityTypeManager
......@@ -112,7 +118,7 @@ class ResourceConfigForm extends EntityForm {
? $this->t($info['label'])
: $info['label'];
}
$form['bundle_wrapper']['bundle_id'] = [
$form['bundle_wrapper']['_bundle_id'] = [
'#type' => 'select',
'#empty_option' => $this->t('- Select -'),
'#title' => $this->t('Bundle'),
......@@ -125,26 +131,22 @@ class ResourceConfigForm extends EntityForm {
];
}
else {
$form['bundle_wrapper']['bundle_id'] = [
$form['bundle_wrapper']['_bundle_id'] = [
'#type' => 'hidden',
'#value' => $entity_type_id,
];
}
$bundle = $has_bundles
? $form_state->getValue('bundle_id')
? $form_state->getValue('_bundle_id')
: $entity_type_id;
if ($resource_type = $this->resourceTypeRepository->get($entity_type_id, $bundle)) {
// Get the JSON API resource type.
$form['bundle_wrapper']['fields_wrapper']['header'] = [
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => $this->t('Override @type', [
'@type' => $resource_type->getTypeName(),
'@entity_type' => $entity_type_id,
'@bundle' => $bundle,
]),
];
$form['bundle_wrapper']['fields_wrapper'] = $this->buildOverridesForm($resource_type);
}
$form['id'] = [
'#type' => 'hidden',
'#value' => sprintf('%s--%s', $entity_type_id, $bundle),
];
return $form;
}
......@@ -184,4 +186,81 @@ class ResourceConfigForm extends EntityForm {
return $form['bundle_wrapper'];
}
/**
* Builds the part of the form that contains the overrides.
*
* @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type
* The resource type being overridden.
*
* @return array
* The partial form.
*/
protected function buildOverridesForm(ResourceType $resource_type) {
$entity_type_id = $resource_type->getEntityTypeId();
$bundle = $resource_type->getBundle();
/** @var FieldDefinitionInterface[] $field_definitions */
$field_definitions = $this->fieldManager->getFieldDefinitions($entity_type_id, $bundle);
$overrides_form['header'] = [
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => $this->t('Override @type', [
'@type' => $resource_type->getTypeName(),
'@entity_type' => $entity_type_id,
'@bundle' => $bundle,
]),
];
$overrides_form['resourceType'] = [
'#type' => 'textfield',
'#title' => $this->t('Resource Type'),
'#description' => $this->t('Overrides the type of the resource. Example: Change "node--article" to "articles".'),
];
$overrides_form['path'] = [
'#type' => 'textfield',
'#title' => $this->t('Resource Path'),
'#description' => $this->t('Overrides the path of the resource. Example: Change "/jsonapi/node/article" to "/jsonapi/articles".'),
];
$overrides_form['resourceFields'] = [
'#type' => 'fieldgroup',
'#title' => $this->t('Resource fields'),
'#description' => $this->t('Override configuration at the field level.'),
'#tree' => TRUE,
];
foreach ($field_definitions as $field_definition) {
$field_name = $field_definition->getName();
$overrides_form['resourceFields'][$field_name] = [
'#type' => 'fieldgroup',
'#title' => $this->t('Overrides for: @field', [
'@field' => $field_name,
]),
];
$overrides_form['resourceFields'][$field_name] += $this->buildOverridesField($field_definition);
}
return $overrides_form;
}
/**
* Builds the part of the form that overrides the field.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition being overridden.
*
* @return array
* The partial form.
*/
protected function buildOverridesField(FieldDefinitionInterface $field_definition) {
$overrides_form = [];
$overrides_form['fieldName'] = [
'#type' => 'hidden',
'#value' => $field_definition->getName(),
];
$overrides_form['publicName'] = [
'#type' => 'textfield',
'#title' => $this->t('Public Name'),
'#description' => $this->t('Overrides the field name with a custom name. Example: Change "field_tags" to "tags".'),
'#default_value' => $field_definition->getName(),
];
return $overrides_form;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment