summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMateu Aguiló Bosch2017-04-23 16:13:09 (GMT)
committerMateu Aguiló Bosch2017-04-23 16:13:09 (GMT)
commit9054173846e4f4b1c75dac488274fe6e92a24546 (patch)
tree3dbc5794de235d067ebafffc9e25165f4920fbd0
parentc865b7e8cdb77307b9f202950193846372acf052 (diff)
feat(Misc): Create config entity from UI
However, the non-bundle entity types are not working yet.
-rw-r--r--README.md3
-rw-r--r--config/schema/jsonapi_extras.schema.yml11
-rw-r--r--src/Form/ResourceConfigForm.php121
3 files changed, 107 insertions, 28 deletions
diff --git a/README.md b/README.md
index d830267..ee7ae81 100644
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/config/schema/jsonapi_extras.schema.yml b/config/schema/jsonapi_extras.schema.yml
index 6144bc6..9ce7c34 100644
--- a/config/schema/jsonapi_extras.schema.yml
+++ b/config/schema/jsonapi_extras.schema.yml
@@ -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'
diff --git a/src/Form/ResourceConfigForm.php b/src/Form/ResourceConfigForm.php
index 0513fba..6932567 100644
--- a/src/Form/ResourceConfigForm.php
+++ b/src/Form/ResourceConfigForm.php
@@ -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;
@@ -32,14 +36,20 @@ 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;
+ }
+
}