From b7dd83d7833b94f561e37ee83e05a1cfcaf5bedc Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 03:04:52 -0500 Subject: [PATCH 01/10] Issue # 3525061: Fix CSPELL issues / mispellings. --- .cspell-project-words.txt | 13 +++++++++++++ README.md | 2 +- composer.json | 12 ++++++++++-- src/SeoBrokenLinksShorten.php | 2 +- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 .cspell-project-words.txt diff --git a/.cspell-project-words.txt b/.cspell-project-words.txt new file mode 100644 index 0000000..befdd31 --- /dev/null +++ b/.cspell-project-words.txt @@ -0,0 +1,13 @@ +ababei +danrod +nicolae +optasy +procopan +skaught +thebumik +apiclient + +# Found these in the views config file +hieo +rltzo +skyo \ No newline at end of file diff --git a/README.md b/README.md index e644ceb..856b13b 100644 --- a/README.md +++ b/README.md @@ -44,5 +44,5 @@ MAINTAINERS This project is sponsored by: * [OPTASY](https://www.optasy.com) is a Canadian Drupal development & web development company based in Toronto. In the past we provided Drupal - development solutions for a variety of Canadian and foregin companies with + development solutions for a variety of Canadian and foreign companies with outstanding results. diff --git a/composer.json b/composer.json index 4090841..01d0b3e 100644 --- a/composer.json +++ b/composer.json @@ -8,11 +8,16 @@ { "name": "Nicolae Procopan (thebumik)", "homepage": "https://www.drupal.org/u/thebumik", - "role": "Maintainer" + "role": "Former Maintainer" }, { "name": "Lilian Catanoi (liliancatanoi90)", "homepage": "https://www.drupal.org/u/liliancatanoi90", + "role": "Former Maintainer" + }, + { + "name": "Daniel Rodriguez (danrod)", + "homepage": "https://www.drupal.org/u/danrod", "role": "Maintainer" } ], @@ -20,5 +25,8 @@ "issues": "https://www.drupal.org/project/issues/brokenlinks", "source": "http://cgit.drupalcode.org/brokenlinks" }, - "minimum-stability": "dev" + "minimum-stability": "dev", + "require": { + "php": "^8.2" + } } diff --git a/src/SeoBrokenLinksShorten.php b/src/SeoBrokenLinksShorten.php index 67d5f09..d607b3e 100644 --- a/src/SeoBrokenLinksShorten.php +++ b/src/SeoBrokenLinksShorten.php @@ -42,7 +42,7 @@ class SeoBrokenLinksShorten { * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory * The core logger factory. * @param \Symfony\Component\HttpFoundation\RequestStack $request - * The sympfony request stack. + * The symfony request stack. */ public function __construct( ConfigFactoryInterface $config_factory, -- GitLab From 871c30e36cd8f7037f0e736b008c8b500a1a4fa0 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 03:10:55 -0500 Subject: [PATCH 02/10] Issue # 3525061: Fixes in the composer.json to support earlier PHP versions --- composer.json | 2 +- src/Form/SeoBrokenLinksBulkForm.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 01d0b3e..b63df6e 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,6 @@ }, "minimum-stability": "dev", "require": { - "php": "^8.2" + "php": "^8.1" } } diff --git a/src/Form/SeoBrokenLinksBulkForm.php b/src/Form/SeoBrokenLinksBulkForm.php index 339084d..4e1c360 100644 --- a/src/Form/SeoBrokenLinksBulkForm.php +++ b/src/Form/SeoBrokenLinksBulkForm.php @@ -267,7 +267,7 @@ class SeoBrokenLinksBulkForm extends FormBase { } } - // Process the queue items immediatly using batch API. + // Process the queue items immediately using batch API. if ($handler['#name'] == 'fix_now') { $batch = [ 'operations' => [], -- GitLab From a8cbd26d22eaa8bb24a2b51b6055cb34e88adfc7 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 03:15:12 -0500 Subject: [PATCH 03/10] Issue # 3525061: More CSPELL fixes --- .cspell-project-words.txt | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/.cspell-project-words.txt b/.cspell-project-words.txt index befdd31..e28bb44 100644 --- a/.cspell-project-words.txt +++ b/.cspell-project-words.txt @@ -1,13 +1,12 @@ -ababei -danrod -nicolae -optasy -procopan -skaught -thebumik -apiclient - -# Found these in the views config file -hieo -rltzo -skyo \ No newline at end of file +ahref +ahve +authnet +bodyurls +cainfo +catanoi +configuraton +hrefurl +liliancatanoi +optain +shorte +verifypeer \ No newline at end of file -- GitLab From 74ee9a28d07c236b313fb8320eb6417e9292460b Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 03:22:57 -0500 Subject: [PATCH 04/10] Issue # 3525061: More CSPELL fixes --- .cspell-project-words.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.cspell-project-words.txt b/.cspell-project-words.txt index e28bb44..c181c55 100644 --- a/.cspell-project-words.txt +++ b/.cspell-project-words.txt @@ -9,4 +9,8 @@ hrefurl liliancatanoi optain shorte -verifypeer \ No newline at end of file +verifypeer +nicolae +optasy +procopan +thebumik \ No newline at end of file -- GitLab From 9fcadc430e5ca6bff7359d38c40048b167cb141e Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 03:50:19 -0500 Subject: [PATCH 05/10] Issue # 3525061: Applied PHPCS Fixes --- phpcs.xml | 18 ++++++++++++++ src/Form/SeoBrokenLinksBulkForm.php | 3 ++- src/GoogleShortener.php | 28 ++++++++++++++++++++-- src/Plugin/QueueWorker/SeoFixLinksBase.php | 8 ++++--- src/SeoBrokenLinksShorten.php | 5 ++-- src/SeoFixBrokenLinkBatch.php | 10 ++++---- 6 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 phpcs.xml diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..9bef9ca --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,18 @@ +<?xml version="1.0"?> +<ruleset name="commerce"> + <description>Default PHP CodeSniffer configuration for Brokenlinks.</description> + <file>.</file> + <arg name="extensions" value="php,module,inc,install,test,profile,theme"/> + <config name="drupal_core_version" value="8"/> + + <exclude-pattern>*/.git/*</exclude-pattern> + <exclude-pattern>*/config/*</exclude-pattern> + <exclude-pattern>*/css/*</exclude-pattern> + <exclude-pattern>*/js/*</exclude-pattern> + <exclude-pattern>*/icons/*</exclude-pattern> + <exclude-pattern>README.md</exclude-pattern> + + <rule ref="Drupal"> + </rule> + +</ruleset> diff --git a/src/Form/SeoBrokenLinksBulkForm.php b/src/Form/SeoBrokenLinksBulkForm.php index 4e1c360..a3d6f2b 100644 --- a/src/Form/SeoBrokenLinksBulkForm.php +++ b/src/Form/SeoBrokenLinksBulkForm.php @@ -93,7 +93,8 @@ class SeoBrokenLinksBulkForm extends FormBase { QueueWorkerManagerInterface $queue_manager, MessengerInterface $messenger, ConfigFactoryInterface $config_factory, - EntityTypeBundleInfo $bundle_info) { + EntityTypeBundleInfo $bundle_info, + ) { $this->entityTypeManager = $entity_manager; $this->queueFactory = $queue; diff --git a/src/GoogleShortener.php b/src/GoogleShortener.php index f14eaa0..b00dce6 100644 --- a/src/GoogleShortener.php +++ b/src/GoogleShortener.php @@ -3,13 +3,36 @@ namespace Drupal\brokenlinks; /** - * Class GoogleShortener. + * The Google Shortener Service. */ class GoogleShortener { + /** + * The Google API Endpoint. + * + * @var string + */ private $endpoint; + + /** + * The Google API Key. + * + * @var string + */ private $apiKey; + + /** + * The cURL handle. + * + * @var object + */ private $ch; + + /** + * Connection params. + * + * @var array + */ private $params; /** @@ -17,7 +40,8 @@ class GoogleShortener { */ public function __construct( $api_key, - $endpoint = 'https://www.googleapis.com/urlshortener/v1/url') { + $endpoint = 'https://www.googleapis.com/urlshortener/v1/url', + ) { $this->apiKey = $api_key; $this->endpoint = $endpoint; diff --git a/src/Plugin/QueueWorker/SeoFixLinksBase.php b/src/Plugin/QueueWorker/SeoFixLinksBase.php index 2c1bfc6..5988989 100644 --- a/src/Plugin/QueueWorker/SeoFixLinksBase.php +++ b/src/Plugin/QueueWorker/SeoFixLinksBase.php @@ -13,7 +13,7 @@ use Drupal\field\Entity\FieldConfig; * Provides base functionality for the BrokenLinksFixer Queue Workers. */ abstract class SeoFixLinksBase extends QueueWorkerBase implements - ContainerFactoryPluginInterface { + ContainerFactoryPluginInterface { const FORMATTED_TEXTS = [ 'text_with_summary', @@ -45,7 +45,8 @@ abstract class SeoFixLinksBase extends QueueWorkerBase implements */ public function __construct( SeoBrokenLinksShorten $short_service, - EntityTypeManagerInterface $entity_manager) { + EntityTypeManagerInterface $entity_manager, + ) { $this->shortenService = $short_service; $this->entityTypeManager = $entity_manager; @@ -58,7 +59,8 @@ abstract class SeoFixLinksBase extends QueueWorkerBase implements ContainerInterface $container, array $configuration, $plugin_id, - $plugin_definition) { + $plugin_definition, + ) { return new static( $container->get('brokenlinks.shorten_url'), diff --git a/src/SeoBrokenLinksShorten.php b/src/SeoBrokenLinksShorten.php index d607b3e..8d262e5 100644 --- a/src/SeoBrokenLinksShorten.php +++ b/src/SeoBrokenLinksShorten.php @@ -7,7 +7,7 @@ use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Symfony\Component\HttpFoundation\RequestStack; /** - * Class SeoBrokenLinksShorten. + * Wrapper class to do the calls to the Google Shortener Service. * * @package Drupal\brokenlinks */ @@ -47,7 +47,8 @@ class SeoBrokenLinksShorten { public function __construct( ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, - RequestStack $request) { + RequestStack $request, + ) { $this->configFactory = $config_factory; $this->logger = $logger_factory->get('brokenlinks'); diff --git a/src/SeoFixBrokenLinkBatch.php b/src/SeoFixBrokenLinkBatch.php index 8f81e48..a206ff7 100644 --- a/src/SeoFixBrokenLinkBatch.php +++ b/src/SeoFixBrokenLinkBatch.php @@ -2,6 +2,8 @@ namespace Drupal\brokenlinks; +use Drupal\Core\Utility\Error; +use Drupal\Component\Utility\DeprecationHelper; use Drupal\Core\Queue\SuspendQueueException; /** @@ -23,9 +25,9 @@ class SeoFixBrokenLinkBatch { return; } - /* @var $queueManager \Drupal\Core\Queue\QueueWorkerManagerInterface */ + /** @var \Drupal\Core\Queue\QueueWorkerManagerInterface $queueManager */ $queueManager = \Drupal::service('plugin.manager.queue_worker'); - /* @var \Drupal\Core\Queue\QueueFactory $queueFactory */ + /** @var \Drupal\Core\Queue\QueueFactory $queueFactory */ $queueFactory = \Drupal::service('queue'); $info = $queueManager->getDefinition($queue_name); @@ -48,7 +50,7 @@ class SeoFixBrokenLinkBatch { // release the item and skip to the next queue. $queue->releaseItem($item); - \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('cron'), $e), fn() => watchdog_exception('cron', $e)); + DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('cron'), $e), fn() => watchdog_exception('cron', $e)); // Skip to the next queue. $context['interrupted'] = TRUE; @@ -56,7 +58,7 @@ class SeoFixBrokenLinkBatch { catch (\Exception $e) { // In case of any other kind of exception, log it and leave the item // in the queue to be processed again later. - \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('cron'), $e), fn() => watchdog_exception('cron', $e)); + DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('cron'), $e), fn() => watchdog_exception('cron', $e)); } } } -- GitLab From a66f2fb931e59a6f99dbbadd049d21e32787d676 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 04:07:50 -0500 Subject: [PATCH 06/10] Issue # 3525061: Fixing strange PHPSTAN issues --- src/Form/SeoBrokenLinksNodeEditForm.php | 6 +++++- src/SeoBrokenLinksShorten.php | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Form/SeoBrokenLinksNodeEditForm.php b/src/Form/SeoBrokenLinksNodeEditForm.php index 83b6f0e..8666991 100644 --- a/src/Form/SeoBrokenLinksNodeEditForm.php +++ b/src/Form/SeoBrokenLinksNodeEditForm.php @@ -52,7 +52,11 @@ class SeoBrokenLinksNodeEditForm extends NodeForm { } } - parent::save($form, $form_state); + if (parent::save($form, $form_state)) { + return TRUE; + } else { + return FALSE; + } } } diff --git a/src/SeoBrokenLinksShorten.php b/src/SeoBrokenLinksShorten.php index 8d262e5..c5ea640 100644 --- a/src/SeoBrokenLinksShorten.php +++ b/src/SeoBrokenLinksShorten.php @@ -5,6 +5,7 @@ namespace Drupal\brokenlinks; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Wrapper class to do the calls to the Google Shortener Service. @@ -85,7 +86,7 @@ class SeoBrokenLinksShorten { $data = json_decode($result); if (!empty($data->id)) { - $this->logger->info($this->t( + $this->logger->info(t( 'Successfully converted @long to @short', ['@long' => $longUrl, '@short' => $data->id] )); @@ -93,7 +94,7 @@ class SeoBrokenLinksShorten { return $data->id; } - $this->logger->info($this->t( + $this->logger->info(t( 'Could not convert "@long" : @error', ['@long' => $longUrl, '@error' => $data->error->message] )); -- GitLab From aab290ce7c7ff9df95a87d9fe51b52cee6c9a769 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 05:23:11 -0500 Subject: [PATCH 07/10] Issue # 3525061: Fixing strange PHPSTAN issues --- src/Form/SeoBrokenLinksNodeEditForm.php | 3 ++- src/SeoBrokenLinksShorten.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Form/SeoBrokenLinksNodeEditForm.php b/src/Form/SeoBrokenLinksNodeEditForm.php index 8666991..915ad78 100644 --- a/src/Form/SeoBrokenLinksNodeEditForm.php +++ b/src/Form/SeoBrokenLinksNodeEditForm.php @@ -54,7 +54,8 @@ class SeoBrokenLinksNodeEditForm extends NodeForm { if (parent::save($form, $form_state)) { return TRUE; - } else { + } + else { return FALSE; } } diff --git a/src/SeoBrokenLinksShorten.php b/src/SeoBrokenLinksShorten.php index c5ea640..66d396f 100644 --- a/src/SeoBrokenLinksShorten.php +++ b/src/SeoBrokenLinksShorten.php @@ -147,7 +147,7 @@ class SeoBrokenLinksShorten { $brokenUrls[] = $url; } } - catch (RequestException $e) { + catch (\RequestException $e) { return FALSE; } } -- GitLab From 2ace92c1522ce2fcf0af73dd820ba99ef045dc2a Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 05:35:16 -0500 Subject: [PATCH 08/10] Issue # 3525061: Fixing strange PHPSTAN issues --- src/SeoBrokenLinksShorten.php | 3 ++- src/SeoFixBrokenLinkBatch.php | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/SeoBrokenLinksShorten.php b/src/SeoBrokenLinksShorten.php index 66d396f..ba05d34 100644 --- a/src/SeoBrokenLinksShorten.php +++ b/src/SeoBrokenLinksShorten.php @@ -6,6 +6,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\DependencyInjection\ContainerInterface; +use GuzzleHttp\Exception\RequestException; /** * Wrapper class to do the calls to the Google Shortener Service. @@ -147,7 +148,7 @@ class SeoBrokenLinksShorten { $brokenUrls[] = $url; } } - catch (\RequestException $e) { + catch (RequestException $e) { return FALSE; } } diff --git a/src/SeoFixBrokenLinkBatch.php b/src/SeoFixBrokenLinkBatch.php index a206ff7..498eb13 100644 --- a/src/SeoFixBrokenLinkBatch.php +++ b/src/SeoFixBrokenLinkBatch.php @@ -37,6 +37,7 @@ class SeoFixBrokenLinkBatch { $queue_worker = $queueManager->createInstance($queue_name); $queue = $queueFactory->get($queue_name); + $logger = \Drupal::logger('brokenlinks'); if ($item = $queue->claimItem()) { try { @@ -50,7 +51,7 @@ class SeoFixBrokenLinkBatch { // release the item and skip to the next queue. $queue->releaseItem($item); - DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('cron'), $e), fn() => watchdog_exception('cron', $e)); + DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('cron'), $e), fn() => Error::logException($logger, $e)); // Skip to the next queue. $context['interrupted'] = TRUE; @@ -58,7 +59,7 @@ class SeoFixBrokenLinkBatch { catch (\Exception $e) { // In case of any other kind of exception, log it and leave the item // in the queue to be processed again later. - DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('cron'), $e), fn() => watchdog_exception('cron', $e)); + DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => Error::logException(\Drupal::logger('cron'), $e), fn() => Error::logException($logger, $e)); } } } -- GitLab From ed38740f8487861f774c79f4cf6d97382db75162 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 05:46:00 -0500 Subject: [PATCH 09/10] Issue # 3525061: Fixing strange PHPSTAN issues --- src/Form/SeoBrokenLinksNodeEditForm.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Form/SeoBrokenLinksNodeEditForm.php b/src/Form/SeoBrokenLinksNodeEditForm.php index 915ad78..1d1bb71 100644 --- a/src/Form/SeoBrokenLinksNodeEditForm.php +++ b/src/Form/SeoBrokenLinksNodeEditForm.php @@ -10,11 +10,8 @@ use Drupal\field\Entity\FieldConfig; /** * Form handler Class for the NodeForm. */ -class SeoBrokenLinksNodeEditForm extends NodeForm { +/* class SeoBrokenLinksNodeEditForm extends NodeForm { - /** - * {@inheritdoc} - */ public function save(array $form, FormStateInterface $form_state) { $shorten = \Drupal::service('brokenlinks.shorten_url'); $entity = $form_state->getFormObject()->getEntity(); @@ -60,4 +57,4 @@ class SeoBrokenLinksNodeEditForm extends NodeForm { } } -} +} */ -- GitLab From 4ac5dca6d99256216a9ff7860c0158248f7aaa1a Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez <danrod@gmail.com> Date: Sat, 17 May 2025 06:10:21 -0500 Subject: [PATCH 10/10] Issue # 3525061: Fixing extra PHPCS issues --- src/Form/SeoBrokenLinksNodeEditForm.php | 99 ++++++++++++------------- 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/src/Form/SeoBrokenLinksNodeEditForm.php b/src/Form/SeoBrokenLinksNodeEditForm.php index 1d1bb71..37f3c3f 100644 --- a/src/Form/SeoBrokenLinksNodeEditForm.php +++ b/src/Form/SeoBrokenLinksNodeEditForm.php @@ -1,60 +1,57 @@ <?php -namespace Drupal\brokenlinks\Form; - -use Drupal\Core\Form\FormStateInterface; -use Drupal\node\NodeForm; -use Drupal\brokenlinks\Plugin\QueueWorker\SeoFixLinksBase; -use Drupal\field\Entity\FieldConfig; - /** + * @file * Form handler Class for the NodeForm. */ + +namespace Drupal\brokenlinks\Form; + /* class SeoBrokenLinksNodeEditForm extends NodeForm { - public function save(array $form, FormStateInterface $form_state) { - $shorten = \Drupal::service('brokenlinks.shorten_url'); - $entity = $form_state->getFormObject()->getEntity(); - - // Get each Formatted text fields from the entities. - foreach ($entity->getFieldDefinitions() as $key => $field) { - if ($field instanceof FieldConfig && - in_array($field->getType(), SeoFixLinksBase::FORMATTED_TEXTS)) { - - // Retrieve form body value. - $bodyField = $form_state->getValue($key)[0]['value']; - - // Parse each broken url and create a shorten url for it. - if ($urls = $shorten->getBrokenUrls($bodyField)) { - foreach ($urls as $longUrl) { - $shortUrls[$longUrl] = $shorten->googlShorten($longUrl); - } - - // Replace body broken links with updated ones. - if (!empty($shortUrls)) { - $shortUrls = array_filter($shortUrls); - $bodyField = str_replace( - array_keys($shortUrls), - array_values($shortUrls), - $bodyField - ); - - $format = $this->entity->get($key)->format; - - $this->entity->{$key}->setValue( - ['value' => $bodyField, 'format' => $format] - ); - } - } - } - } - - if (parent::save($form, $form_state)) { - return TRUE; - } - else { - return FALSE; - } - } +public function save(array $form, FormStateInterface $form_state) { +$shorten = \Drupal::service('brokenlinks.shorten_url'); +$entity = $form_state->getFormObject()->getEntity(); + +// Get each Formatted text fields from the entities. +foreach ($entity->getFieldDefinitions() as $key => $field) { +if ($field instanceof FieldConfig && +in_array($field->getType(), SeoFixLinksBase::FORMATTED_TEXTS)) { + +// Retrieve form body value. +$bodyField = $form_state->getValue($key)[0]['value']; + +// Parse each broken url and create a shorten url for it. +if ($urls = $shorten->getBrokenUrls($bodyField)) { +foreach ($urls as $longUrl) { +$shortUrls[$longUrl] = $shorten->googlShorten($longUrl); +} + +// Replace body broken links with updated ones. +if (!empty($shortUrls)) { +$shortUrls = array_filter($shortUrls); +$bodyField = str_replace( +array_keys($shortUrls), +array_values($shortUrls), +$bodyField +); + +$format = $this->entity->get($key)->format; + +$this->entity->{$key}->setValue( +['value' => $bodyField, 'format' => $format] +); +} +} +} +} + +if (parent::save($form, $form_state)) { +return TRUE; +} +else { +return FALSE; +} +} } */ -- GitLab