diff --git a/.cspell-project-words.txt b/.cspell-project-words.txt new file mode 100644 index 0000000000000000000000000000000000000000..20f23df52a9f17dcf58dfcba4d0e6bf8a7768d29 --- /dev/null +++ b/.cspell-project-words.txt @@ -0,0 +1 @@ +smartdate \ No newline at end of file diff --git a/config/schema/unified_date.schema.yml b/config/schema/unified_date.schema.yml index 0abb207cf4b7e47facbbc1cd6bba46492681b320..41235efb9a30a84c9e56574c2225f1e5060e7550 100644 --- a/config/schema/unified_date.schema.yml +++ b/config/schema/unified_date.schema.yml @@ -1,6 +1,9 @@ unified_date.settings: - type: sequence + type: config_object label: 'Unified date settings' - sequence: - type: string - label: 'Date field to use for unified date per node type' + mapping: + node_types: + label: 'Node date field mapping' + type: sequence + sequence: + type: string \ No newline at end of file diff --git a/dictionary.txt b/dictionary.txt new file mode 100644 index 0000000000000000000000000000000000000000..30e0206a4c9d54a6a880290c05269c3a176f5c90 --- /dev/null +++ b/dictionary.txt @@ -0,0 +1 @@ +smartdate diff --git a/src/Form/BulkUpdateForm.php b/src/Form/BulkUpdateForm.php index 2c795bd967418708c847b272707383cdecd3e6dc..3dd6c7f7678c1e0cbd6a58c569a07eb6288f6ea0 100644 --- a/src/Form/BulkUpdateForm.php +++ b/src/Form/BulkUpdateForm.php @@ -2,14 +2,36 @@ namespace Drupal\unified_date\Form; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Configure file system settings for this site. */ class BulkUpdateForm extends FormBase { + /** + * Constructs a new bulk update form. + * + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The entity type manager. + */ + public function __construct( + protected EntityTypeManagerInterface $entityTypeManager, + ) { + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity_type.manager') + ); + } + /** * {@inheritdoc} */ @@ -33,10 +55,7 @@ class BulkUpdateForm extends FormBase { '#default_value' => [], ]; - $config = $this->config('unified_date.settings'); - $entity_type_manager = \Drupal::entityTypeManager(); - - $node_types = $entity_type_manager + $node_types = $this->entityTypeManager ->getStorage('node_type') ->loadMultiple(); if ($node_types) { diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index fd31a0fe19018f1453d7e069de940724550837b1..fa6dd8d188a048dd48bb4cdac3776ccf2c94c1c8 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -2,8 +2,10 @@ namespace Drupal\unified_date\Form; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\unified_date\UnifiedDateManager; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -12,27 +14,27 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class SettingsForm extends ConfigFormBase { /** - * The entity field manager. + * Constructs a new bulk update form. * - * @var \Drupal\Core\Entity\EntityFieldManagerInterface + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The entity type manager. + * @param \Drupal\unified_date\UnifiedDateManager $unifiedDateManager + * The unified date manager. */ - protected $entityTypeManager; - - /** - * The unified date manager. - * - * @var \Drupal\unified_date\UnifiedDateManager - */ - protected $unifiedDateManager; + public function __construct( + protected EntityTypeManagerInterface $entityTypeManager, + protected UnifiedDateManager $unifiedDateManager, + ) { + } /** - * {@inheritDoc} + * {@inheritdoc} */ public static function create(ContainerInterface $container) { - $instance = parent::create($container); - $instance->entityTypeManager = $container->get('entity_type.manager'); - $instance->unifiedDateManager = $container->get('unified_date.manager'); - return $instance; + return new static( + $container->get('entity_type.manager'), + $container->get('unified_date.manager') + ); } /** @@ -69,7 +71,7 @@ class SettingsForm extends ConfigFormBase { ]), '#description' => $this->t('All node types without date fields selected will used the "Created" date.'), ]; - if ($config && $default_value = $config->get($node_type)) { + if ($config && $default_value = $config->get('node_types.' . $node_type)) { $form[$node_type]['#default_value'] = $default_value; } } @@ -82,8 +84,7 @@ class SettingsForm extends ConfigFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $config = $this->config('unified_date.settings'); - $this->entityTypeManager = \Drupal::entityTypeManager(); + $config = $this->config('unified_date.settings'); // Loop through node types and store field selection in config. $node_types = $this->entityTypeManager @@ -91,7 +92,7 @@ class SettingsForm extends ConfigFormBase { ->loadMultiple(); if ($node_types) { foreach (array_keys($node_types) as $node_type) { - $config->set($node_type, $form_state->getValue($node_type)); + $config->set('node_types.' . $node_type, $form_state->getValue($node_type)); } } diff --git a/src/UnifiedDateBatchProcessor.php b/src/UnifiedDateBatchProcessor.php index 26bb420e8959b97f13046686baab9c120cfa6788..2654778872fdcc47bc6f9b7f282c05b1ba8e0620 100644 --- a/src/UnifiedDateBatchProcessor.php +++ b/src/UnifiedDateBatchProcessor.php @@ -75,6 +75,7 @@ class UnifiedDateBatchProcessor { ->sort('nid', 'ASC') ->execute(); if ($nids) { + /** @var \Drupal\unified_date\UnifiedDateManager $unified_date_manager */ $unified_date_manager = \Drupal::service('unified_date.manager'); if ($nodes = Node::loadMultiple($nids)) { foreach ($nodes as $node) { @@ -125,7 +126,7 @@ class UnifiedDateBatchProcessor { } else { $message = t('An error occurred while processing the Unified Dates with arguments : @args', [ - '@args' => print_r($error_operation[0]), + '@args' => reset($operations), ]); \Drupal::service('messenger')->addMessage($message); } diff --git a/src/UnifiedDateManager.php b/src/UnifiedDateManager.php index faa177e7a15154c2f96bfae17c71359e47c2a454..1f8c3d3813c9b64ae147ab9d7678a4d303ca7baa 100644 --- a/src/UnifiedDateManager.php +++ b/src/UnifiedDateManager.php @@ -20,33 +20,24 @@ class UnifiedDateManager { * * @var array */ - protected $configRawData = []; - - /** - * The entity field manager. - * - * @var \Drupal\Core\Entity\EntityFieldManagerInterface - */ - protected EntityFieldManagerInterface $entityFieldManager; - - /** - * The module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected ModuleHandlerInterface $moduleHandler; + protected array $configRawData = []; /** * UnifiedDateManager constructor. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory + * The config factory. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager + * The entity field manager. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler + * The module handler. */ public function __construct( - ConfigFactoryInterface $config_factory, - EntityFieldManagerInterface $entity_field_manager, - ModuleHandlerInterface $module_handler + protected ConfigFactoryInterface $configFactory, + protected EntityFieldManagerInterface $entityFieldManager, + protected ModuleHandlerInterface $moduleHandler, ) { - $this->configRawData = $config_factory->get('unified_date.settings')->getRawData(); - $this->entityFieldManager = $entity_field_manager; - $this->moduleHandler = $module_handler; + $this->configRawData = $this->configFactory->get('unified_date.settings')->getRawData(); } /** @@ -64,12 +55,14 @@ class UnifiedDateManager { // Reload the configuration if in a test environment. // This ensures that we can more efficiently test. if (function_exists('drupal_valid_test_ua') && drupal_valid_test_ua()) { - $config_factory = \Drupal::configFactory(); - $this->configRawData = $config_factory->get('unified_date.settings')->getRawData(); + $this->configRawData = $this->configFactory->get('unified_date.settings')->getRawData(); } // Field where date is stored. - $field = $this->configRawData[$node_type] ?? 'base-field:created'; + $field = 'base-field:created'; + if (isset($this->configRawData['node_types'][$node_type])) { + $field = $this->configRawData['node_types'][$node_type]; + } // Strip base-field prefix. $field = str_replace('base-field:', '', $field); diff --git a/tests/modules/unified_date_test_config/config/install/core.entity_form_display.node.publication.default.yml b/tests/modules/unified_date_test_config/config/install/core.entity_form_display.node.publication.default.yml index 341e2be7bd99b9dbf3fa6bafa850c05cde1f34c7..92cabb7c1669b18771732da42ad9e68b46076008 100644 --- a/tests/modules/unified_date_test_config/config/install/core.entity_form_display.node.publication.default.yml +++ b/tests/modules/unified_date_test_config/config/install/core.entity_form_display.node.publication.default.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: config: diff --git a/tests/modules/unified_date_test_config/config/install/core.entity_view_display.node.publication.default.yml b/tests/modules/unified_date_test_config/config/install/core.entity_view_display.node.publication.default.yml index 5101c75301b98748539c826b979ecde069ad910e..60bf9343191c36365b7f00fb27d22e983ba25664 100644 --- a/tests/modules/unified_date_test_config/config/install/core.entity_view_display.node.publication.default.yml +++ b/tests/modules/unified_date_test_config/config/install/core.entity_view_display.node.publication.default.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: config: diff --git a/tests/modules/unified_date_test_config/config/install/core.entity_view_display.node.publication.teaser.yml b/tests/modules/unified_date_test_config/config/install/core.entity_view_display.node.publication.teaser.yml index ee710f3ee222c1082757cb237fcdd1dc499a5f29..0619b69f72dde255023ed903901410f186c4d18f 100644 --- a/tests/modules/unified_date_test_config/config/install/core.entity_view_display.node.publication.teaser.yml +++ b/tests/modules/unified_date_test_config/config/install/core.entity_view_display.node.publication.teaser.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: config: diff --git a/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date.yml b/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date.yml index dccf0d893a5fa6ce3ec720250e007c3d884b8d43..bf8be1e3e1ff535f70abf0042bebf94700ca0397 100644 --- a/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date.yml +++ b/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: config: diff --git a/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date_and_time.yml b/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date_and_time.yml index 8e1e20c85b4b7f3d1f69daa07a1727864f846d74..793e5a7a4b594ef1ffa380dd2a40aacca35fc58c 100644 --- a/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date_and_time.yml +++ b/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date_and_time.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: config: diff --git a/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date_range.yml b/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date_range.yml index eb7b08f9d1f2e75f2aec71a4a2497daf4a405e5a..5cb15883ed7ec8c70b324993a0d4f0fac519be06 100644 --- a/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date_range.yml +++ b/tests/modules/unified_date_test_config/config/install/field.field.node.publication.field_publication_date_range.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: config: diff --git a/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date.yml b/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date.yml index f334ff2521d7901988370191101051917cd19aa1..59694ec44d6ccd4d538b2efb292449569981b72f 100644 --- a/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date.yml +++ b/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: module: diff --git a/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date_and_time.yml b/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date_and_time.yml index a5e577787c7d4588eb053b336206139cc4e25daa..00ae9c35682a56a3ad88309b900a800d06ddd09a 100644 --- a/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date_and_time.yml +++ b/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date_and_time.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: module: diff --git a/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date_range.yml b/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date_range.yml index 2b85e262281b6c37e6975c8c800c95e0dd787709..77a803f040716fc6036044c118e2e837bdc300b8 100644 --- a/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date_range.yml +++ b/tests/modules/unified_date_test_config/config/install/field.storage.node.field_publication_date_range.yml @@ -1,4 +1,3 @@ -langcode: en status: true dependencies: module: diff --git a/tests/modules/unified_date_test_config/config/install/node.type.publication.yml b/tests/modules/unified_date_test_config/config/install/node.type.publication.yml index 99f21620250c1841ff82f35c0f748e6415341c48..8ed25241e36fa4722b08fa8e1fb8b4a0fd51e833 100644 --- a/tests/modules/unified_date_test_config/config/install/node.type.publication.yml +++ b/tests/modules/unified_date_test_config/config/install/node.type.publication.yml @@ -1,4 +1,3 @@ -langcode: en status: true name: Publication type: publication diff --git a/tests/src/Kernel/UnifiedDateFieldTest.php b/tests/src/Kernel/UnifiedDateFieldTest.php index df2abccd398fc2f4fd40b08f79c82d590b17c9fe..ebdd97e1faa5c29c0fe5a0b0ea008142dfbcb79d 100644 --- a/tests/src/Kernel/UnifiedDateFieldTest.php +++ b/tests/src/Kernel/UnifiedDateFieldTest.php @@ -7,7 +7,7 @@ use Drupal\node\Entity\NodeType; use Drupal\Tests\node\Traits\NodeCreationTrait; /** - * Unified date test base field existance. + * Unified date test base field existence. * * @group unified_date */ @@ -81,7 +81,7 @@ class UnifiedDateFieldTest extends KernelTestBase { */ public function testBasefield(): void { $node = $this->createNode([ - 'title' => t('Hello, world!'), + 'title' => 'Hello, world!', 'type' => 'article', ]); $unified_date_value = $node->get('unified_date')->value; @@ -112,7 +112,7 @@ class UnifiedDateFieldTest extends KernelTestBase { $midnight_three_days_from_now = strtotime('midnight +3 days'); $node = $this->createNode([ - 'title' => t('Publication 1'), + 'title' => 'Publication 1', 'type' => 'publication', ]); $unified_date_value = $node->get('unified_date')->value; @@ -124,7 +124,7 @@ class UnifiedDateFieldTest extends KernelTestBase { // Now change the config to use field_publication_date. $config_factory = \Drupal::configFactory(); $config_factory->getEditable('unified_date.settings') - ->set('publication', 'field_publication_date') + ->set('node_types.publication', 'field_publication_date') ->save(); $node->set('field_publication_date', date('Y-m-d', $midnight_today)); $node->save(); @@ -133,7 +133,7 @@ class UnifiedDateFieldTest extends KernelTestBase { // Now change the config to use field_publication_date_and_time. $config_factory = \Drupal::configFactory(); $config_factory->getEditable('unified_date.settings') - ->set('publication', 'field_publication_date_and_time') + ->set('node_types.publication', 'field_publication_date_and_time') ->save(); $node->set('field_publication_date_and_time', date('Y-m-d\TH:i:s', $one_day_from_now)); $node->save(); @@ -142,7 +142,7 @@ class UnifiedDateFieldTest extends KernelTestBase { // Now change the config to use field_publication_date_and_time start date. $config_factory = \Drupal::configFactory(); $config_factory->getEditable('unified_date.settings') - ->set('publication', 'field_publication_date_range') + ->set('node_types.publication', 'field_publication_date_range') ->save(); $node->set('field_publication_date_range', [ 'value' => date('Y-m-d\TH:i:s', $midnight_two_days_from_now), @@ -154,7 +154,7 @@ class UnifiedDateFieldTest extends KernelTestBase { // Now change the config to use field_publication_date_and_time end date. $config_factory = \Drupal::configFactory(); $config_factory->getEditable('unified_date.settings') - ->set('publication', 'field_publication_date_range:end_value') + ->set('node_types.publication', 'field_publication_date_range:end_value') ->save(); $node->set('field_publication_date_range', [ 'value' => date('Y-m-d\TH:i:s', $one_day_from_now), diff --git a/unified_date.info.yml b/unified_date.info.yml index c2333b7b719974cd534ddc662139659962a6fbfc..c91103f0e57400a33fc958dd7d1d49e95365e05b 100644 --- a/unified_date.info.yml +++ b/unified_date.info.yml @@ -1,7 +1,7 @@ name: 'Unified date' type: module description: 'A module to store a date field value in a unified date field on the base node table for multiple content type listing sorting.' -core_version_requirement: ^9.0 || ^10.0 +core_version_requirement: ^9.0 || ^10.0 || ^11 package: 'Soapbox' configure: unified_date.settings dependencies: diff --git a/unified_date.install b/unified_date.install new file mode 100644 index 0000000000000000000000000000000000000000..bcb586575cc59fe87deba9e4f12b0d9e0516d6e1 --- /dev/null +++ b/unified_date.install @@ -0,0 +1,20 @@ +<?php + +/** + * @file + * Install file for unified_date. + */ + +/** + * Update the unified date schema to respect the schema mapping. + * + * @see https://www.drupal.org/node/2442603 + */ +function unified_date_update_10001() { + $config = \Drupal::configFactory()->getEditable('unified_date.settings'); + if (empty($config->get('node_types'))) { + $raw_data = $config->getRawData(); + $config->set('node_types', $raw_data); + $config->save(); + } +} diff --git a/unified_date.module b/unified_date.module index 721b7744d15559ef7bb701eb0e6ae6b398e95816..ee1a4555971bf503abc283f5f4c595a2051199b1 100644 --- a/unified_date.module +++ b/unified_date.module @@ -55,6 +55,7 @@ function unified_date_entity_base_field_info(EntityTypeInterface $entity_type) { function unified_date_node_presave(EntityInterface $entity) { // Get the date only, don't save. Entity will be saved in the subsequent // lifecycle of the entity. + /** @var \Drupal\unified_date\UnifiedDateManager $unified_date_manager */ $unified_date_manager = \Drupal::service('unified_date.manager'); $entity->set('unified_date', $unified_date_manager->getUnifiedDate($entity)); }