Skip to content

Commit

Permalink
Additional data types added to the data dictionary form (#4173)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaise-lafrai authored May 21, 2024
1 parent a0306c6 commit 013f2c1
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ private static function createType() {
'#options' => [
'string' => t('String'),
'date' => t('Date'),
'datetime' => t('Datetime'),
'integer' => t('Integer'),
'number' => t('Number'),
'year' => t('Year'),
'boolean' => t('Boolean'),
],
'#ajax' => [
'callback' => '\Drupal\data_dictionary_widget\Fields\FieldCallbacks::updateFormatOptions',
Expand All @@ -83,7 +86,7 @@ private static function createFormat() {
'#type' => 'select',
'#required' => TRUE,
'#title' => 'Format',
'#description' => FieldOperations::generateFormatDescription("string"),
'#description' => FieldOperations::generateFormats("string", "description"),
'#default_value' => 'default',
'#prefix' => '<div id = field-json-metadata-format>',
'#suffix' => '</div>',
Expand Down
4 changes: 2 additions & 2 deletions modules/data_dictionary_widget/src/Fields/FieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static function updateFormatOptions(array &$form, FormStateInterface $for
$data_type = $field[0]["dictionary_fields"]["data"][$field_index]["field_collection"]["type"] ?? 'string';
}

$format_field['#description'] = FieldOperations::generateFormatDescription($data_type);
$options = FieldOperations::setFormatOptions($data_type);
$format_field['#description'] = FieldOperations::generateFormats($data_type, "description");
$options = FieldOperations::generateFormats($data_type, "options");

$format_field["#options"] = $options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ private static function createType($key, $current_fields) {
* Create Format field.
*/
private static function createFormat($key, $current_fields) {
$format_options = FieldOperations::setFormatOptions($current_fields[$key]['type']);
$value = in_array($current_fields[$key]['format'], $format_options) ? $current_fields[$key]['format'] : 'other';
$format_options = FieldOperations::generateFormats($current_fields[$key]['type'], "options");
$value = in_array($current_fields[$key]['format'], $format_options, TRUE) ? $current_fields[$key]['format'] : 'other';
return [
'#name' => 'field_json_metadata[0][dictionary_fields][data][' . $key . '][field_collection][format]',
'#type' => 'select',
'#required' => TRUE,
'#title' => 'Format',
'#default_value' => 'default',
'#description' => FieldOperations::generateFormatDescription($current_fields[$key]['type']),
'#description' => FieldOperations::generateFormats($current_fields[$key]['type'], "description"),
'#value' => $value,
'#prefix' => '<div id = field-json-metadata-' . $key . '-format>',
'#suffix' => '</div>',
Expand All @@ -84,7 +84,7 @@ private static function createFormat($key, $current_fields) {
* Create Format Other field.
*/
private static function createFormatOther($key, $current_fields) {
$format_options = FieldOperations::setFormatOptions($current_fields[$key]['type']);
$format_options = FieldOperations::generateFormats($current_fields[$key]['type'], "options");
$value = !in_array($current_fields[$key]['format'], $format_options) ? $current_fields[$key]['format'] : NULL;

return [
Expand Down
98 changes: 51 additions & 47 deletions modules/data_dictionary_widget/src/Fields/FieldOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,60 +62,51 @@ public static function setAjaxElements(array $dictionaryFields) {
*
* @param string $dataType
* Field data type.
* @param string $property
* Property of array.
*
* @return string
* Description information.
* @return string|array
* Description information or options list.
*
* @throws \InvalidArgumentException
*/
public static function generateFormatDescription($dataType) {
public static function generateFormats($dataType, $property) {
$description = "<p>The format of the data in this field. Supported formats depend on the specified field type:</p>";

if ($dataType === 'string') {
$description .= FieldValues::returnStringInfo('description');
}
switch ($dataType) {
case 'string':
$info = FieldValues::returnStringInfo($property);
break;

if ($dataType === 'date') {
$description = FieldValues::returnDateInfo('description');
}
case 'date':
$info = FieldValues::returnDateInfo($property);
break;

if ($dataType === 'integer') {
$description .= FieldValues::returnIntegerInfo('description');
}
case 'datetime':
$info = FieldValues::returnDateTimeInfo($property);
break;

if ($dataType === 'number') {
$description .= FieldValues::returnNumberInfo('description');
}
return $description;
}
case 'integer':
$info = FieldValues::returnIntegerInfo($property);
break;

/**
* Function to generate the options for the "Format" field.
*
* @param string $dataType
* Field data type.
*
* @return array
* List of format options.
*/
public static function setFormatOptions($dataType) {
case 'number':
$info = FieldValues::returnNumberInfo($property);
break;

if ($dataType === 'string') {
$options = FieldValues::returnStringInfo('options');
}
case 'year':
$info = FieldValues::returnYearInfo($property);
break;

if ($dataType === 'date') {
$options = FieldValues::returnDateInfo('options');
}

if ($dataType === 'integer') {
$options = FieldValues::returnIntegerInfo('options');
}
case 'boolean':
$info = FieldValues::returnBooleanInfo($property);
break;

if ($dataType === 'number') {
$options = FieldValues::returnNumberInfo('options');
default:
throw new \InvalidArgumentException("Unexpected data type: $dataType");
}

return $options;

return ($property === "description") ? ($description . $info) : $info;
}

/**
Expand Down Expand Up @@ -169,8 +160,11 @@ public static function setTypeOptions() {
return [
'string' => t('String'),
'date' => t('Date'),
'datetime' => t('Datetime'),
'integer' => t('Integer'),
'number' => t('Number'),
'year' => t('Year'),
'boolean' => t('Boolean'),
];
}

Expand Down Expand Up @@ -290,8 +284,8 @@ public static function resetAllFormatOptions(array &$form, FormStateInterface $f
]);

if ($type) {
$form["field_json_metadata"]["widget"][0]["dictionary_fields"]["edit_fields"][$index]["format"]["#options"] = FieldOperations::setFormatOptions($type);
$form["field_json_metadata"]["widget"][0]["dictionary_fields"]["edit_fields"][$index]["format"]["#description"] = FieldOperations::generateFormatDescription($type);
$form["field_json_metadata"]["widget"][0]["dictionary_fields"]["edit_fields"][$index]["format"]["#options"] = FieldOperations::generateFormats($type, "options");
$form["field_json_metadata"]["widget"][0]["dictionary_fields"]["edit_fields"][$index]["format"]["#description"] = FieldOperations::generateFormats($type, "description");
}
}

Expand All @@ -310,10 +304,20 @@ public static function resetAllFormatOptions(array &$form, FormStateInterface $f
* The form array to be modified.
*/
public static function resetDateFormatOptions(array &$form) {
$field_collection = isset($form["field_json_metadata"]["widget"][0]["dictionary_fields"]["field_collection"]);
if ($field_collection && $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["field_collection"]["group"]["type"]["#value"] === "date") {
$form["field_json_metadata"]["widget"][0]["dictionary_fields"]["field_collection"]["group"]["format"]["#options"] = FieldValues::returnDateInfo('options');
$form["field_json_metadata"]["widget"][0]["dictionary_fields"]["field_collection"]["group"]["format"]["#description"] = FieldValues::returnDateInfo('description');
$data_type = $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["field_collection"]["group"]["type"]["#value"] ?? NULL;

if (!$data_type || ($data_type !== "date" && $data_type !== "datetime")) {
return;
}

$options_method = ($data_type === "date") ? 'returnDateInfo' : 'returnDateTimeInfo';
$options = FieldValues::$options_method('options');
$description = FieldValues::$options_method('description');

if ($options && $description) {
$format_field =& $form["field_json_metadata"]["widget"][0]["dictionary_fields"]["field_collection"]["group"]["format"];
$format_field["#options"] = $options;
$format_field["#description"] = $description;
}
}

Expand Down
66 changes: 61 additions & 5 deletions modules/data_dictionary_widget/src/Fields/FieldValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function updateValues($field_index, $update_values, $current_field
}

/**
* Retrun information about the string field option.
* Return information about the string field option.
*/
public static function returnStringInfo($type) {
if ($type == 'options') {
Expand All @@ -52,7 +52,7 @@ public static function returnStringInfo($type) {
}

/**
* Retrun information about the date field option.
* Return information about the date field option.
*/
public static function returnDateInfo($type) {
if ($type == 'options') {
Expand All @@ -74,7 +74,29 @@ public static function returnDateInfo($type) {
}

/**
* Retrun information about the integer field option.
* Return information about the datetime field option.
*/
public static function returnDateTimeInfo($type) {
if ($type == 'options') {
return [
'default' => 'default',
'any' => 'any',
'other' => 'other',
];
}
elseif ($type == 'description') {
return "
<ul>
<li><b>default</b>: An ISO8601 format string of datetime.</li>
<li><b>any</b>: Any parsable representation of a date. The implementing library can attept to parse the datetime via a range of strategies.</li>
<li><b>other</b>: If your date values follow a collective but non-ISO8601 pattern, select this option and define the incoming format using the syntax of <a href='https://strftime.org/'>C / Python strftime</a>.
For example, if your data had dates formatted as MM/DD/YYYY, you would enter %m/%d/%Y into the Other format field.</li>
</ul>";
}
}

/**
* Return information about the integer field option.
*/
public static function returnIntegerInfo($type) {
if ($type == 'options') {
Expand All @@ -91,7 +113,7 @@ public static function returnIntegerInfo($type) {
}

/**
* Retrun information about the number field option.
* Return information about the number field option.
*/
public static function returnNumberInfo($type) {
if ($type == 'options') {
Expand All @@ -102,7 +124,41 @@ public static function returnNumberInfo($type) {
elseif ($type == 'description') {
return "
<ul>
<li><b>default</b>: Any valid string.</li>
<li><b>default</b>: An exact fixed-point number. No non-numeric characters allowed other than the decimal.</li>
</ul>";
}
}

/**
* Return information about the year field option.
*/
public static function returnYearInfo($type) {
if ($type == 'options') {
return [
'default' => 'default',
];
}
elseif ($type == 'description') {
return "
<ul>
<li><b>default</b>: 4-digit numbers in the range 1901 to 2155.</li>
</ul>";
}
}

/**
* Return information about the year field option.
*/
public static function returnBooleanInfo($type) {
if ($type == 'options') {
return [
'default' => 'default',
];
}
elseif ($type == 'description') {
return "
<ul>
<li><b>default</b>: 1/0 values, or True/False values (not case sensitive).</li>
</ul>";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

use PHPUnit\Framework\TestCase;
use Drupal\data_dictionary_widget\Fields\FieldAddCreation;
use Drupal\data_dictionary_widget\Fields\FieldButtons;
use Drupal\data_dictionary_widget\Fields\FieldCallbacks;
use Drupal\data_dictionary_widget\Fields\FieldCreation;
use Drupal\data_dictionary_widget\Fields\FieldEditCreation;
use Drupal\data_dictionary_widget\Fields\FieldOperations;
use Drupal\data_dictionary_widget\Fields\FieldValues;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityFormInterface;
Expand Down Expand Up @@ -212,6 +209,8 @@ public function testAddNewFieldDictionaryWidget() {
]
];



$dataDictionaryWidget = new DataDictionaryWidget (
$plugin_id,
$plugin_definition,
Expand Down Expand Up @@ -392,8 +391,8 @@ public function testEditDataDictionaryField() {
$user_input = [
'field_json_metadata' => [
0 => [
'identifier' => 'Kaise',
'title' => 'Kaise',
'identifier' => 'test_identifier',
'title' => 'test_title',
'dictionary_fields' => [
'data' => [
0 => [
Expand Down
Loading

0 comments on commit 013f2c1

Please sign in to comment.