Skip to content

Commit

Permalink
Data-Dictionary Creation Form (#3806)
Browse files Browse the repository at this point in the history
  • Loading branch information
Clayton Liddell authored Jul 20, 2022
1 parent 69090f0 commit cfc7fa9
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
field.widget.settings.json_form_widget:
type: mapping
label: 'JSON Form widget settings'
mapping:
schema:
type: string
label: 'Schema'
mapping: { }
8 changes: 3 additions & 5 deletions modules/json_form_widget/src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -21,11 +23,16 @@
* "string_long"
* }
* )
*
* @codeCoverageIgnore
*/
class JsonFormWidget extends WidgetBase {

/**
* Default DKAN Data Schema.
*
* @var string
*/
protected const DEFAULT_SCHEMA = 'dataset';

/**
* FormBuilder.
*
Expand All @@ -40,6 +47,13 @@ class JsonFormWidget extends WidgetBase {
*/
protected $valueHandler;

/**
* DKAN Data Schema.
*
* @var string|null
*/
protected ?string $schema;

/**
* Constructs a WidgetBase object.
*
Expand All @@ -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');
}

/**
Expand All @@ -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}
*/
Expand All @@ -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];
}
Expand All @@ -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'];
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 22 additions & 5 deletions modules/metastore/metastore.links.menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
10 changes: 9 additions & 1 deletion modules/metastore/metastore.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
35 changes: 31 additions & 4 deletions modules/metastore/src/LifeCycle/LifeCycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit cfc7fa9

Please sign in to comment.