From b40d053b77e3f24da83c9eae392eb22dc17bfd3c Mon Sep 17 00:00:00 2001 From: Project Update Bot <66574-Project-Update-Bot@users.noreply.drupalcode.org> Date: Tue, 18 Jun 2024 22:20:20 +0000 Subject: [PATCH] Issue #3435210 by scott_euser: Automated Drupal 11 compatibility fixes for unified_date --- .cspell-project-words.txt | 1 + config/schema/unified_date.schema.yml | 11 +++-- dictionary.txt | 1 + src/Form/BulkUpdateForm.php | 27 ++++++++++-- src/Form/SettingsForm.php | 39 +++++++++--------- src/UnifiedDateBatchProcessor.php | 3 +- src/UnifiedDateManager.php | 41 ++++++++----------- ..._form_display.node.publication.default.yml | 1 - ..._view_display.node.publication.default.yml | 1 - ...y_view_display.node.publication.teaser.yml | 1 - ...ode.publication.field_publication_date.yml | 1 - ...cation.field_publication_date_and_time.yml | 1 - ...blication.field_publication_date_range.yml | 1 - ...ld.storage.node.field_publication_date.yml | 1 - ...e.node.field_publication_date_and_time.yml | 1 - ...rage.node.field_publication_date_range.yml | 1 - .../config/install/node.type.publication.yml | 1 - tests/src/Kernel/UnifiedDateFieldTest.php | 14 +++---- unified_date.info.yml | 2 +- unified_date.install | 20 +++++++++ unified_date.module | 1 + 21 files changed, 100 insertions(+), 70 deletions(-) create mode 100644 .cspell-project-words.txt create mode 100644 dictionary.txt create mode 100644 unified_date.install diff --git a/.cspell-project-words.txt b/.cspell-project-words.txt new file mode 100644 index 0000000..20f23df --- /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 0abb207..41235ef 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 0000000..30e0206 --- /dev/null +++ b/dictionary.txt @@ -0,0 +1 @@ +smartdate diff --git a/src/Form/BulkUpdateForm.php b/src/Form/BulkUpdateForm.php index 2c795bd..3dd6c7f 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 fd31a0f..fa6dd8d 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 26bb420..2654778 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 faa177e..1f8c3d3 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 341e2be..92cabb7 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 5101c75..60bf934 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 ee710f3..0619b69 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 dccf0d8..bf8be1e 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 8e1e20c..793e5a7 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 eb7b08f..5cb1588 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 f334ff2..59694ec 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 a5e5777..00ae9c3 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 2b85e26..77a803f 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 99f2162..8ed2524 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 df2abcc..ebdd97e 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 c2333b7..c91103f 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 0000000..bcb5865 --- /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 721b774..ee1a455 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)); } -- GitLab