Skip to content

Commit

Permalink
updating patch for jsonapi_extras
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfinnarn committed Feb 23, 2024
1 parent 9df5ac4 commit aeb24f2
Showing 1 changed file with 241 additions and 2 deletions.
243 changes: 241 additions & 2 deletions patches/field-type-enhancer-3025283-3.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,205 @@
diff --git a/src/Form/JsonapiExtrasFieldTypesForm.php b/src/Form/JsonapiExtrasFieldTypesForm.php
new file mode 100644
index 0000000..6b9dee2
--- /dev/null
+++ b/src/Form/JsonapiExtrasFieldTypesForm.php
@@ -0,0 +1,194 @@
+<?php
+
+namespace Drupal\jsonapi_extras\Form;
+
+use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\Entity\EntityFieldManager;
+use Drupal\Core\Form\FormBase;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Base form for jsonapi_resource_config.
+ */
+class JsonapiExtrasFieldTypesForm extends FormBase {
+
+ /**
+ * The field manager.
+ *
+ * @var \Drupal\Core\Entity\EntityFieldManager
+ */
+ protected $fieldManager;
+
+ /**
+ * The field enhancer manager.
+ *
+ * @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager
+ */
+ protected $enhancerManager;
+
+ /**
+ * @var \Drupal\Core\Config\ConfigFactoryInterface
+ */
+ private ConfigFactoryInterface $configManager;
+
+ /**
+ * JsonapiResourceConfigForm constructor.
+ *
+ * @param \Drupal\Core\Entity\EntityFieldManager $field_manager
+ * The entity field manager.
+ * @param \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager $enhancer_manager
+ */
+ public function __construct(EntityFieldManager $field_manager, ResourceFieldEnhancerManager $enhancer_manager, ConfigFactoryInterface $config_manager) {
+ $this->fieldManager = $field_manager;
+ $this->enhancerManager = $enhancer_manager;
+ $this->configManager = $config_manager;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('entity_field.manager'),
+ $container->get('plugin.manager.resource_field_enhancer'),
+ $container->get('config.factory')
+ );
+ }
+
+ public function getFormId() {
+ return 'jsonapi_extras_field_types_form';
+ }
+
+ public function buildForm(array $form, FormStateInterface $form_state): array {
+ // Get current field type configuration.
+ $config = $this->configManager->get('jsonapi_extras.jsonapi_field_type_config');
+
+ // Collect unique field types.
+ $field_types = $this->fieldManager->getFieldMap();
+ $unique_field_types = [];
+ foreach ($field_types as $fields) {
+ foreach ($fields as $field_info) {
+ $unique_field_types[$field_info['type']] = $field_info['type'];
+ }
+ }
+
+ // Sort field types alphabetically by key.
+ ksort($unique_field_types);
+
+ // Get all the enhancers.
+ $options = $this->enhancerManager->getDefinitions();
+
+ // Get all option labels.
+ $labels = array_map(fn($option) => $option['label'], $options);
+
+ // Add a none option.
+ $options = array_merge(['- None -' => $this->t('- None -')], $options);
+ $labels = array_merge(['- None -'], $labels);
+
+ // Add a form element for each field type.
+ foreach ($unique_field_types as $field_type) {
+ $form['field_types'][$field_type] = [
+ '#type' => 'container',
+ '#title' => $field_type,
+ '#suffix' => '<hr>',
+ '#attributes' => ['style' =>
+ 'display: flex; gap: 1rem; flex-direction: column;'],
+ ];
+
+ // Add name of the field type.
+ $form['field_types'][$field_type][$field_type . '_name'] = [
+ '#markup' => "<div><strong>Field Type:</strong> $field_type</div>",
+ ];
+
+ // Get enhancer from configuration.
+ $resource_field = $config->get('resourceFields')[$field_type];
+
+ $form['field_types'][$field_type][$field_type . '_enhancer_id'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Enhancer'),
+ '#options' => array_combine(array_keys($options), $labels),
+ '#default_value' => $resource_field['enhancer']['id'],
+ ];
+
+ $form['field_types'][$field_type][$field_type . '_enhancer_settings'] = [
+ '#type' => 'container',
+ ];
+
+ // Add a container for each option.
+ foreach ($options as $key => $option) {
+ if ($key === '- None -') {
+ continue;
+ }
+
+ $form['field_types'][$field_type][$field_type . '_enhancer_settings'][$key] = [
+ '#type' => 'container',
+ '#states' => [
+ 'visible' => [
+ ':input[name="' . $field_type . '_enhancer_id"]' => ['value' => $key],
+ ],
+ ],
+ ];
+
+ /** @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface $enhancer */
+ $enhancer = $this->enhancerManager->createInstance($key);
+ $stuff = $enhancer->getSettingsForm($resource_field);
+
+ // Add field type and enhancer id prefix to the settings.
+ // e.g. 'link__url_link__absolute_url'
+ $settings = [];
+ foreach ($stuff as $setting_key => $setting) {
+ $settings[$field_type . '__' . $key . '__' . $setting_key] = $setting;
+ }
+
+ $form['field_types'][$field_type][$field_type . '_enhancer_settings'][$key][] = $settings;
+ }
+
+ }
+
+ // Add submit button.
+ $form['submit'] = [
+ '#type' => 'submit',
+ '#value' => $this->t('Save Configuration'),
+ ];
+
+ return $form;
+ }
+
+ public function submitForm(array &$form, FormStateInterface $form_state): void {
+ $values = $form_state->getValues();
+
+ // Filter values to only include enhancer ids.
+ $enhancer_ids = array_filter($values, fn($value, $key) => str_contains($key, '_enhancer_id'), ARRAY_FILTER_USE_BOTH);
+
+ // Loop through enhancer ids and look for settings related to each field type.
+ $field_types = [];
+ foreach ($enhancer_ids as $type => $enhancer_id) {
+ $field_type = str_replace('_enhancer_id', '', $type);
+ $field_types[$field_type] = [
+ 'fieldName' => $field_type,
+ 'enhancer' => ['id' => $enhancer_id],
+ ];
+ // No need to process the none option.
+ if ($enhancer_id === '- None -') {
+ $field_types[$field_type]['enhancer']['id'] = '';
+ continue;
+ }
+
+ // Filter values to only include settings related to the current field type.
+ $settings = array_filter($values, fn($value, $key) => str_contains($key, $field_type . '__' . $enhancer_id), ARRAY_FILTER_USE_BOTH);
+ if (!empty($settings)) {
+ // Remove the prefix from the settings keys.
+ // Preserve the $key as the index in the array.
+ $settings = array_combine(array_map(fn($key) => str_replace($field_type . '__' . $enhancer_id . '__', '', $key), array_keys($settings)), $settings);
+ $field_types[$field_type]['enhancer']['settings'] = $settings;
+ }
+ }
+
+ $config = $this->configManager->getEditable('jsonapi_extras.jsonapi_field_type_config');
+ $config->set('resourceFields', $field_types);
+ $config->save();
+ }
+
+}
diff --git a/src/Normalizer/FieldItemNormalizer.php b/src/Normalizer/FieldItemNormalizer.php
index 8882870..9f8abc8 100644
index 8882870..b496b8c 100644
--- a/src/Normalizer/FieldItemNormalizer.php
+++ b/src/Normalizer/FieldItemNormalizer.php
@@ -51,12 +51,30 @@ class FieldItemNormalizer extends JsonApiNormalizerDecoratorBase {
Expand All @@ -17,7 +217,7 @@ index 8882870..9f8abc8 100644
+ // Look for default field type enhancer.
+ $config = \Drupal::config('jsonapi_extras.jsonapi_field_type_config');
+ $field_type = $object->getFieldDefinition()->getType();
+ $field_type_config = $config->get('mapping.resourceFieldTypes')[$field_type];
+ $field_type_config = $config->get('resourceFields')[$field_type];
+
+ if (!empty($field_type_config['enhancer']['id'])) {
+ // Get the enhancer instance.
Expand All @@ -34,3 +234,42 @@ index 8882870..9f8abc8 100644
}
$cacheability = CacheableMetadata::createFromObject($normalized_output)
->addCacheTags(['config:jsonapi_resource_config_list']);
diff --git a/src/Plugin/DateTimeEnhancerBase.php b/src/Plugin/DateTimeEnhancerBase.php
index 770efe3..df02bdf 100644
--- a/src/Plugin/DateTimeEnhancerBase.php
+++ b/src/Plugin/DateTimeEnhancerBase.php
@@ -38,7 +38,7 @@ abstract class DateTimeEnhancerBase extends ResourceFieldEnhancerBase {
'#type' => 'textfield',
'#title' => $this->t('Format'),
'#description' => $this->t('Use a valid date format.'),
- '#default_value' => $settings['dateTimeFormat'],
+ '#default_value' => $settings['dateTimeFormat'] ?? $this->defaultConfiguration()['dateTimeFormat'],
],
];
}
diff --git a/src/Plugin/jsonapi/FieldEnhancer/SingleNestedEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/SingleNestedEnhancer.php
index c712bf5..b570fad 100644
--- a/src/Plugin/jsonapi/FieldEnhancer/SingleNestedEnhancer.php
+++ b/src/Plugin/jsonapi/FieldEnhancer/SingleNestedEnhancer.php
@@ -82,7 +82,7 @@ class SingleNestedEnhancer extends ResourceFieldEnhancerBase {
'#type' => 'textfield',
'#title' => $this->t('Path'),
'#description' => $this->t('A dot separated path to extract the sub-property.'),
- '#default_value' => $settings['path'],
+ '#default_value' => $settings['path'] ?? $this->defaultConfiguration()['path'],
],
];
}
diff --git a/src/Plugin/jsonapi/FieldEnhancer/UrlLinkEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/UrlLinkEnhancer.php
index 0cf8ba5..ece4bcb 100644
--- a/src/Plugin/jsonapi/FieldEnhancer/UrlLinkEnhancer.php
+++ b/src/Plugin/jsonapi/FieldEnhancer/UrlLinkEnhancer.php
@@ -94,7 +94,7 @@ class UrlLinkEnhancer extends ResourceFieldEnhancerBase implements ContainerFact
$form['absolute_url'] = [
'#type' => 'checkbox',
'#title' => $this->t('Get Absolute Urls'),
- '#default_value' => $settings['absolute_url'],
+ '#default_value' => $settings['absolute_url'] ?? $this->defaultConfiguration()['absolute_url'],
];

return $form;

0 comments on commit aeb24f2

Please sign in to comment.