Skip to content
Snippets Groups Projects
Commit b40d053b authored by project update bot's avatar project update bot Committed by Scott Euser
Browse files

Issue #3435210 by scott_euser: Automated Drupal 11 compatibility fixes for unified_date

parent 2133c33e
No related branches found
No related tags found
1 merge request!1Automated Project Update Bot fixes
Pipeline #202427 passed
Showing
with 99 additions and 70 deletions
smartdate
\ No newline at end of file
unified_date.settings: unified_date.settings:
type: sequence type: config_object
label: 'Unified date settings' label: 'Unified date settings'
sequence: mapping:
type: string node_types:
label: 'Date field to use for unified date per node type' label: 'Node date field mapping'
type: sequence
sequence:
type: string
\ No newline at end of file
smartdate
...@@ -2,14 +2,36 @@ ...@@ -2,14 +2,36 @@
namespace Drupal\unified_date\Form; namespace Drupal\unified_date\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Configure file system settings for this site. * Configure file system settings for this site.
*/ */
class BulkUpdateForm extends FormBase { 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} * {@inheritdoc}
*/ */
...@@ -33,10 +55,7 @@ class BulkUpdateForm extends FormBase { ...@@ -33,10 +55,7 @@ class BulkUpdateForm extends FormBase {
'#default_value' => [], '#default_value' => [],
]; ];
$config = $this->config('unified_date.settings'); $node_types = $this->entityTypeManager
$entity_type_manager = \Drupal::entityTypeManager();
$node_types = $entity_type_manager
->getStorage('node_type') ->getStorage('node_type')
->loadMultiple(); ->loadMultiple();
if ($node_types) { if ($node_types) {
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
namespace Drupal\unified_date\Form; namespace Drupal\unified_date\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\unified_date\UnifiedDateManager;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -12,27 +14,27 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -12,27 +14,27 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase { 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; public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
/** protected UnifiedDateManager $unifiedDateManager,
* The unified date manager. ) {
* }
* @var \Drupal\unified_date\UnifiedDateManager
*/
protected $unifiedDateManager;
/** /**
* {@inheritDoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
$instance = parent::create($container); return new static(
$instance->entityTypeManager = $container->get('entity_type.manager'); $container->get('entity_type.manager'),
$instance->unifiedDateManager = $container->get('unified_date.manager'); $container->get('unified_date.manager')
return $instance; );
} }
/** /**
...@@ -69,7 +71,7 @@ class SettingsForm extends ConfigFormBase { ...@@ -69,7 +71,7 @@ class SettingsForm extends ConfigFormBase {
]), ]),
'#description' => $this->t('All node types without date fields selected will used the "Created" date.'), '#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; $form[$node_type]['#default_value'] = $default_value;
} }
} }
...@@ -82,8 +84,7 @@ class SettingsForm extends ConfigFormBase { ...@@ -82,8 +84,7 @@ class SettingsForm extends ConfigFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('unified_date.settings'); $config = $this->config('unified_date.settings');
$this->entityTypeManager = \Drupal::entityTypeManager();
// Loop through node types and store field selection in config. // Loop through node types and store field selection in config.
$node_types = $this->entityTypeManager $node_types = $this->entityTypeManager
...@@ -91,7 +92,7 @@ class SettingsForm extends ConfigFormBase { ...@@ -91,7 +92,7 @@ class SettingsForm extends ConfigFormBase {
->loadMultiple(); ->loadMultiple();
if ($node_types) { if ($node_types) {
foreach (array_keys($node_types) as $node_type) { 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));
} }
} }
......
...@@ -75,6 +75,7 @@ class UnifiedDateBatchProcessor { ...@@ -75,6 +75,7 @@ class UnifiedDateBatchProcessor {
->sort('nid', 'ASC') ->sort('nid', 'ASC')
->execute(); ->execute();
if ($nids) { if ($nids) {
/** @var \Drupal\unified_date\UnifiedDateManager $unified_date_manager */
$unified_date_manager = \Drupal::service('unified_date.manager'); $unified_date_manager = \Drupal::service('unified_date.manager');
if ($nodes = Node::loadMultiple($nids)) { if ($nodes = Node::loadMultiple($nids)) {
foreach ($nodes as $node) { foreach ($nodes as $node) {
...@@ -125,7 +126,7 @@ class UnifiedDateBatchProcessor { ...@@ -125,7 +126,7 @@ class UnifiedDateBatchProcessor {
} }
else { else {
$message = t('An error occurred while processing the Unified Dates with arguments : @args', [ $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); \Drupal::service('messenger')->addMessage($message);
} }
......
...@@ -20,33 +20,24 @@ class UnifiedDateManager { ...@@ -20,33 +20,24 @@ class UnifiedDateManager {
* *
* @var array * @var array
*/ */
protected $configRawData = []; protected array $configRawData = [];
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected EntityFieldManagerInterface $entityFieldManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected ModuleHandlerInterface $moduleHandler;
/** /**
* UnifiedDateManager constructor. * 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( public function __construct(
ConfigFactoryInterface $config_factory, protected ConfigFactoryInterface $configFactory,
EntityFieldManagerInterface $entity_field_manager, protected EntityFieldManagerInterface $entityFieldManager,
ModuleHandlerInterface $module_handler protected ModuleHandlerInterface $moduleHandler,
) { ) {
$this->configRawData = $config_factory->get('unified_date.settings')->getRawData(); $this->configRawData = $this->configFactory->get('unified_date.settings')->getRawData();
$this->entityFieldManager = $entity_field_manager;
$this->moduleHandler = $module_handler;
} }
/** /**
...@@ -64,12 +55,14 @@ class UnifiedDateManager { ...@@ -64,12 +55,14 @@ class UnifiedDateManager {
// Reload the configuration if in a test environment. // Reload the configuration if in a test environment.
// This ensures that we can more efficiently test. // This ensures that we can more efficiently test.
if (function_exists('drupal_valid_test_ua') && drupal_valid_test_ua()) { if (function_exists('drupal_valid_test_ua') && drupal_valid_test_ua()) {
$config_factory = \Drupal::configFactory(); $this->configRawData = $this->configFactory->get('unified_date.settings')->getRawData();
$this->configRawData = $config_factory->get('unified_date.settings')->getRawData();
} }
// Field where date is stored. // 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. // Strip base-field prefix.
$field = str_replace('base-field:', '', $field); $field = str_replace('base-field:', '', $field);
......
langcode: en
status: true status: true
dependencies: dependencies:
config: config:
......
langcode: en
status: true status: true
dependencies: dependencies:
config: config:
......
langcode: en
status: true status: true
dependencies: dependencies:
config: config:
......
langcode: en
status: true status: true
dependencies: dependencies:
config: config:
......
langcode: en
status: true status: true
dependencies: dependencies:
config: config:
......
langcode: en
status: true status: true
dependencies: dependencies:
config: config:
......
langcode: en
status: true status: true
dependencies: dependencies:
module: module:
......
langcode: en
status: true status: true
dependencies: dependencies:
module: module:
......
langcode: en
status: true status: true
dependencies: dependencies:
module: module:
......
langcode: en
status: true status: true
name: Publication name: Publication
type: publication type: publication
......
...@@ -7,7 +7,7 @@ use Drupal\node\Entity\NodeType; ...@@ -7,7 +7,7 @@ use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\NodeCreationTrait; use Drupal\Tests\node\Traits\NodeCreationTrait;
/** /**
* Unified date test base field existance. * Unified date test base field existence.
* *
* @group unified_date * @group unified_date
*/ */
...@@ -81,7 +81,7 @@ class UnifiedDateFieldTest extends KernelTestBase { ...@@ -81,7 +81,7 @@ class UnifiedDateFieldTest extends KernelTestBase {
*/ */
public function testBasefield(): void { public function testBasefield(): void {
$node = $this->createNode([ $node = $this->createNode([
'title' => t('Hello, world!'), 'title' => 'Hello, world!',
'type' => 'article', 'type' => 'article',
]); ]);
$unified_date_value = $node->get('unified_date')->value; $unified_date_value = $node->get('unified_date')->value;
...@@ -112,7 +112,7 @@ class UnifiedDateFieldTest extends KernelTestBase { ...@@ -112,7 +112,7 @@ class UnifiedDateFieldTest extends KernelTestBase {
$midnight_three_days_from_now = strtotime('midnight +3 days'); $midnight_three_days_from_now = strtotime('midnight +3 days');
$node = $this->createNode([ $node = $this->createNode([
'title' => t('Publication 1'), 'title' => 'Publication 1',
'type' => 'publication', 'type' => 'publication',
]); ]);
$unified_date_value = $node->get('unified_date')->value; $unified_date_value = $node->get('unified_date')->value;
...@@ -124,7 +124,7 @@ class UnifiedDateFieldTest extends KernelTestBase { ...@@ -124,7 +124,7 @@ class UnifiedDateFieldTest extends KernelTestBase {
// Now change the config to use field_publication_date. // Now change the config to use field_publication_date.
$config_factory = \Drupal::configFactory(); $config_factory = \Drupal::configFactory();
$config_factory->getEditable('unified_date.settings') $config_factory->getEditable('unified_date.settings')
->set('publication', 'field_publication_date') ->set('node_types.publication', 'field_publication_date')
->save(); ->save();
$node->set('field_publication_date', date('Y-m-d', $midnight_today)); $node->set('field_publication_date', date('Y-m-d', $midnight_today));
$node->save(); $node->save();
...@@ -133,7 +133,7 @@ class UnifiedDateFieldTest extends KernelTestBase { ...@@ -133,7 +133,7 @@ class UnifiedDateFieldTest extends KernelTestBase {
// Now change the config to use field_publication_date_and_time. // Now change the config to use field_publication_date_and_time.
$config_factory = \Drupal::configFactory(); $config_factory = \Drupal::configFactory();
$config_factory->getEditable('unified_date.settings') $config_factory->getEditable('unified_date.settings')
->set('publication', 'field_publication_date_and_time') ->set('node_types.publication', 'field_publication_date_and_time')
->save(); ->save();
$node->set('field_publication_date_and_time', date('Y-m-d\TH:i:s', $one_day_from_now)); $node->set('field_publication_date_and_time', date('Y-m-d\TH:i:s', $one_day_from_now));
$node->save(); $node->save();
...@@ -142,7 +142,7 @@ class UnifiedDateFieldTest extends KernelTestBase { ...@@ -142,7 +142,7 @@ class UnifiedDateFieldTest extends KernelTestBase {
// Now change the config to use field_publication_date_and_time start date. // Now change the config to use field_publication_date_and_time start date.
$config_factory = \Drupal::configFactory(); $config_factory = \Drupal::configFactory();
$config_factory->getEditable('unified_date.settings') $config_factory->getEditable('unified_date.settings')
->set('publication', 'field_publication_date_range') ->set('node_types.publication', 'field_publication_date_range')
->save(); ->save();
$node->set('field_publication_date_range', [ $node->set('field_publication_date_range', [
'value' => date('Y-m-d\TH:i:s', $midnight_two_days_from_now), 'value' => date('Y-m-d\TH:i:s', $midnight_two_days_from_now),
...@@ -154,7 +154,7 @@ class UnifiedDateFieldTest extends KernelTestBase { ...@@ -154,7 +154,7 @@ class UnifiedDateFieldTest extends KernelTestBase {
// Now change the config to use field_publication_date_and_time end date. // Now change the config to use field_publication_date_and_time end date.
$config_factory = \Drupal::configFactory(); $config_factory = \Drupal::configFactory();
$config_factory->getEditable('unified_date.settings') $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(); ->save();
$node->set('field_publication_date_range', [ $node->set('field_publication_date_range', [
'value' => date('Y-m-d\TH:i:s', $one_day_from_now), 'value' => date('Y-m-d\TH:i:s', $one_day_from_now),
......
name: 'Unified date' name: 'Unified date'
type: module 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.' 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' package: 'Soapbox'
configure: unified_date.settings configure: unified_date.settings
dependencies: dependencies:
......
<?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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment