diff --git a/modules/json_form_widget/config/schema/json_form_widget.schema.yml b/modules/json_form_widget/config/schema/json_form_widget.schema.yml index 528c483b89..e1d68796f6 100644 --- a/modules/json_form_widget/config/schema/json_form_widget.schema.yml +++ b/modules/json_form_widget/config/schema/json_form_widget.schema.yml @@ -1,7 +1,4 @@ field.widget.settings.json_form_widget: type: mapping label: 'JSON Form widget settings' - mapping: - schema: - type: string - label: 'Schema' + mapping: { } diff --git a/modules/json_form_widget/src/FormBuilder.php b/modules/json_form_widget/src/FormBuilder.php index 497dd02929..0c0efa3575 100644 --- a/modules/json_form_widget/src/FormBuilder.php +++ b/modules/json_form_widget/src/FormBuilder.php @@ -74,13 +74,11 @@ public function __construct(SchemaRetriever $schema_retriever, FieldTypeRouter $ /** * Set schema. * - * @codeCoverageIgnore + * @param string $schema_name + * Metadata schema name. */ - public function setSchema($schema_name, $type = NULL) { + public function setSchema(string $schema_name): void { try { - if (!empty($type) && $type !== $schema_name) { - $schema_name = $type; - } $schema = $this->schemaRetriever->retrieve($schema_name); $this->schema = json_decode($schema); $this->schemaUiHandler->setSchemaUi($schema_name); diff --git a/modules/json_form_widget/src/Plugin/Field/FieldWidget/JsonFormWidget.php b/modules/json_form_widget/src/Plugin/Field/FieldWidget/JsonFormWidget.php index 646c89e4f4..144b07c670 100644 --- a/modules/json_form_widget/src/Plugin/Field/FieldWidget/JsonFormWidget.php +++ b/modules/json_form_widget/src/Plugin/Field/FieldWidget/JsonFormWidget.php @@ -2,12 +2,14 @@ namespace Drupal\json_form_widget\Plugin\Field\FieldWidget; +use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; use Drupal\json_form_widget\FormBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Http\RequestStack; use Drupal\json_form_widget\ValueHandler; /** @@ -21,11 +23,16 @@ * "string_long" * } * ) - * - * @codeCoverageIgnore */ class JsonFormWidget extends WidgetBase { + /** + * Default DKAN Data Schema. + * + * @var string + */ + protected const DEFAULT_SCHEMA = 'dataset'; + /** * FormBuilder. * @@ -40,6 +47,13 @@ class JsonFormWidget extends WidgetBase { */ protected $valueHandler; + /** + * DKAN Data Schema. + * + * @var string|null + */ + protected ?string $schema; + /** * Constructs a WidgetBase object. * @@ -57,11 +71,23 @@ class JsonFormWidget extends WidgetBase { * The JsonFormBuilder service. * @param \Drupal\json_form_widget\ValueHandler $value_handler * The JsonFormValueHandler service. + * @param \Drupal\Core\Http\RequestStack $request_stack + * Drupal request context service. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, FormBuilder $builder, ValueHandler $value_handler) { + public function __construct( + $plugin_id, + $plugin_definition, + FieldDefinitionInterface $field_definition, + array $settings, + array $third_party_settings, + FormBuilder $builder, + ValueHandler $value_handler, + RequestStack $request_stack + ) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); $this->builder = $builder; $this->valueHandler = $value_handler; + $this->schema = $request_stack->getCurrentRequest()->query->get('schema'); } /** @@ -75,44 +101,11 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['settings'], $configuration['third_party_settings'], $container->get('json_form.builder'), - $container->get('json_form.value_handler') + $container->get('json_form.value_handler'), + $container->get('request_stack') ); } - /** - * {@inheritdoc} - */ - public static function defaultSettings() { - return [ - 'schema' => 'dataset', - ] + parent::defaultSettings(); - } - - /** - * {@inheritdoc} - */ - public function settingsForm(array $form, FormStateInterface $form_state) { - $element['schema'] = [ - '#type' => 'textfield', - '#title' => $this->t('JSON Schema name'), - '#default_value' => $this->getSetting('schema'), - '#required' => TRUE, - ]; - - return $element; - } - - /** - * {@inheritdoc} - */ - public function settingsSummary() { - $summary = []; - - $summary[] = $this->t('JSON Schema Name: @schema', ['@schema' => $this->getSetting('schema')]); - - return $summary; - } - /** * {@inheritdoc} */ @@ -123,12 +116,16 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen foreach ($items as $item) { $default_data = json_decode($item->value); } - $type = $form_state->getformObject()->getEntity()->get('field_data_type')->value; - $type = isset($type) ? $type : $this->getSetting('schema'); - $this->builder->setSchema($this->getSetting('schema'), $type); - $this->schema = $this->builder->getSchema(); + $type = $this->getSchemaId($form_state); + // Copy the item type to the entity. + $form_entity = $form_state->getFormObject()->getEntity(); + if ($form_entity instanceof FieldableEntityInterface) { + $form_entity->set('field_data_type', $type); + } + // Set the schema for the form builder. + $this->builder->setSchema($type); + // Attempt to build the form. $json_form = $this->builder->getJsonForm($default_data, $form_state); - if ($json_form) { return ['value' => $json_form]; } @@ -138,13 +135,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen * {@inheritdoc} */ public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) { + // Set the schema for the form builder. + $this->builder->setSchema($this->getSchemaId($form_state)); + $field_name = $form_state->get('json_form_widget_field'); - // @todo there is duplicated code here. - $type = $form_state->getformObject()->getEntity()->get('field_data_type')->value; - $type = isset($type) ? $type : $this->getSetting('schema'); - $this->builder->setSchema($this->getSetting('schema'), $type); $schema = $this->builder->getSchema(); - $data = []; $properties = array_keys((array) $schema->properties); $values = $form_state->getValue($field_name)[0]['value']; @@ -168,4 +163,26 @@ public function extractFormValues(FieldItemListInterface $items, array $form, Fo static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state); } + /** + * Get form data schema ID. + * + * @param \Drupal\Core\Form\FormStateInterface|null $form_state + * Form state. + * + * @return string + * Data schema ID. + */ + protected function getSchemaId(?FormStateInterface $form_state = NULL): string { + // Extract the metastore item type from form state if provided. + if (isset($form_state)) { + $form_entity = $form_state->getFormObject()->getEntity(); + if ($form_entity instanceof FieldableEntityInterface && isset($form_entity->field_data_type->value)) { + return $form_entity->field_data_type->value; + } + } + // Otherwise use form state provided in request query, or the default + // schema if one is not found. + return $this->schema ?? self::DEFAULT_SCHEMA; + } + } diff --git a/modules/metastore/config/optional/core.entity_form_display.node.data.default.yml b/modules/metastore/config/optional/core.entity_form_display.node.data.default.yml index 447adf4969..3d9bd3f676 100644 --- a/modules/metastore/config/optional/core.entity_form_display.node.data.default.yml +++ b/modules/metastore/config/optional/core.entity_form_display.node.data.default.yml @@ -22,8 +22,7 @@ content: third_party_settings: { } field_json_metadata: weight: 6 - settings: - schema: dataset + settings: { } third_party_settings: { } type: json_form_widget region: content diff --git a/modules/metastore/metastore.links.menu.yml b/modules/metastore/metastore.links.menu.yml index 9024136ace..753cdaa252 100644 --- a/modules/metastore/metastore.links.menu.yml +++ b/modules/metastore/metastore.links.menu.yml @@ -5,9 +5,26 @@ dkan.metastore.config_properties: parent: system.admin_dkan weight: 14 -datastore.data_dictionary_settings_form: - title: Data-Dictionary settings - description: Data-Dictionary settings. +metastore.data_dictionary: + title: Data-Dictionary + description: Data-Dictionary Pages parent: system.admin_dkan - route_name: datastore.data_dictionary_settings - weight: 13 \ No newline at end of file + route_name: metastore.data_dictionary + weight: 13 + +metastore.data_dictionary.create: + title: Create + description: Create Data-Dictionary + parent: metastore.data_dictionary + route_name: node.add + route_parameters: + node_type: data + schema: data-dictionary + weight: 1 + +metastore.data_dictionary.settings_form: + title: Settings + description: Data-Dictionary Settings + parent: metastore.data_dictionary + route_name: metastore.data_dictionary.settings + weight: 2 \ No newline at end of file diff --git a/modules/metastore/metastore.routing.yml b/modules/metastore/metastore.routing.yml index 01d8a3bf62..3efafae0b0 100644 --- a/modules/metastore/metastore.routing.yml +++ b/modules/metastore/metastore.routing.yml @@ -159,8 +159,16 @@ dkan.metastore.config_properties: options: _admin_route: TRUE -datastore.data_dictionary_settings: +metastore.data_dictionary: path: '/admin/dkan/data-dictionary' + defaults: + _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage' + _title: 'Data-Dictionary' + requirements: + _permission: 'administer site configuration' + +metastore.data_dictionary.settings: + path: '/admin/dkan/data-dictionary/settings' defaults: _title: 'Data-Dictionary Settings' _form: '\Drupal\metastore\Form\DataDictionarySettingsForm' diff --git a/modules/metastore/src/LifeCycle/LifeCycle.php b/modules/metastore/src/LifeCycle/LifeCycle.php index ca4cefc0dd..1038c1a189 100644 --- a/modules/metastore/src/LifeCycle/LifeCycle.php +++ b/modules/metastore/src/LifeCycle/LifeCycle.php @@ -102,9 +102,14 @@ public function __construct( * Metastore item object. */ public function go($stage, MetastoreItemInterface $data) { + // Removed dashes from schema ID since function names can't include dashes. + $schema_id = str_replace('-', '', $data->getSchemaId()); $stage = ucwords($stage); - $method = "{$data->getSchemaId()}{$stage}"; + // Build method name from schema ID and stage. + $method = "{$schema_id}{$stage}"; + // Ensure a method exists for this life cycle stage. if (method_exists($this, $method)) { + // Call life cycle method on metastore item. $this->{$method}($data); } } @@ -248,12 +253,25 @@ private function createResourceReference(Resource $resource): object { } /** - * Private. + * Dataset pre-save life cycle method. + * + * @param \Drupal\metastore\MetastoreItemInterface $data + * Dataset metastore item. */ - protected function datasetPresave(MetastoreItemInterface $data) { + protected function datasetPresave(MetastoreItemInterface $data): void { + $this->referenceMetadata($data); + } + + /** + * Sanitize and reference metadata. + * + * @param \Drupal\metastore\MetastoreItemInterface $data + * Metastore item. + */ + protected function referenceMetadata(MetastoreItemInterface $data): void { $metadata = $data->getMetaData(); - $title = isset($metadata->title) ? $metadata->title : $metadata->name; + $title = $metadata->title ?? $metadata->name; $data->setTitle($title); // If there is no uuid add one. @@ -278,7 +296,16 @@ protected function datasetPresave(MetastoreItemInterface $data) { $raw = $data->getRawMetadata(); $this->orphanChecker->processReferencesInUpdatedDataset($raw, $metadata); } + } + /** + * Data-Dictionary pre-save life cycle method. + * + * @param \Drupal\metastore\MetastoreItemInterface $data + * Data-Dictionary metastore item. + */ + protected function datadictionaryPresave(MetastoreItemInterface $data): void { + $this->referenceMetadata($data); } /**