From b84110fda1d39b7266151ebdbbba4d8c3442364c Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Sun, 7 Jan 2024 12:45:34 -0800 Subject: [PATCH 1/4] Added checks for page info to avoid fatal errors. --- src/Utility.php | 87 ++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/src/Utility.php b/src/Utility.php index 19ebae44..130841b4 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -127,55 +127,62 @@ public static function inList($item, array $list) { * The markup with the page info. */ public static function getPageInfo(array $urls = NULL) : string { - // Default to the current page. - if (!$urls) { - $urls = [self::getUrl()]; - } - - $client = \Drupal::service('quant_api.client'); - $response = $client->getUrlMeta($urls); - - if (isset($response['global_meta']['records'])) { - // Show meta information for the pages in Quant. - $found_urls = []; - $output = '
'; - $output .= '

' . t('Quant Page Info') . '

'; - foreach ($response['global_meta']['records'] as $record) { - $found_urls[] = $url = $record['meta']['url']; - $output .= '
'; - $output .= 'Page info for ' . $url . ''; - $output .= '
    '; - $output .= '
  • Published: ' . ($record['meta']['published'] ? t('Yes') : t('No')) . '
  • '; - $output .= '
  • Revisions: ' . $record['meta']['revision_count'] . '
  • '; - $date = DrupalDateTime::createFromTimestamp($record['meta']['content_timestamp'])->format('Y-m-d H:i:s'); - $output .= '
  • Updated: ' . $date . '
  • '; - $date = DrupalDateTime::createFromTimestamp($record['meta']['date_timestamp'])->format('Y-m-d H:i:s'); - $output .= '
  • Synced: ' . $date . '
  • '; - $output .= '
'; - $output .= '
'; + try { + // Default to the current page. + if (!$urls) { + $urls = [self::getUrl()]; } - // Note any URLs that were not in Quant. - if (count($urls) != count($found_urls)) { - if (count($urls) === 1) { - $output .= '' . t('Page info could not be found for this URL:') . ''; + $client = \Drupal::service('quant_api.client'); + $response = $client->getUrlMeta($urls); + + if (isset($response['global_meta']['records'])) { + // Show meta information for the pages in Quant. + $found_urls = []; + $output = '
'; + $output .= '

' . t('Quant Page Info') . '

'; + foreach ($response['global_meta']['records'] as $record) { + $found_urls[] = $url = ($record['meta']['url'] ?? 'Url unknown'); + $output .= '
'; + $output .= 'Page info for ' . $url . ''; + $output .= '
    '; + $output .= '
  • Published: ' . (($record['meta']['published'] ?? FALSE) ? t('Yes') : t('No')) . '
  • '; + $output .= '
  • Revisions: ' . ($record['meta']['revision_count'] ?? 0). '
  • '; + $date = DrupalDateTime::createFromTimestamp($record['meta']['content_timestamp'] ?? 0)->format('Y-m-d H:i:s'); + $output .= '
  • Updated: ' . $date . '
  • '; + $date = DrupalDateTime::createFromTimestamp($record['meta']['date_timestamp'] ?? 0)->format('Y-m-d H:i:s'); + $output .= '
  • Synced: ' . $date . '
  • '; + $output .= '
'; + $output .= '
'; } - else { - $output .= '' . t('Page info could not be found for the following URLs:') . ''; + + // Note any URLs that were not in Quant. + if (count($urls) != count($found_urls)) { + if (count($urls) === 1) { + $output .= '' . t('Page info could not be found for this URL:') . ''; + } + else { + $output .= '' . t('Page info could not be found for the following URLs:') . ''; + } + $output .= '
    '; } - $output .= '
      '; - } - foreach ($urls as $url) { - if (!in_array($url, $found_urls)) { - $output .= '
    • ' . $url . '
    • '; + foreach ($urls as $url) { + if (!in_array($url, $found_urls)) { + $output .= '
    • ' . $url . '
    • '; + } + $output .= '
    '; } - $output .= '
'; + + $output .= '
'; } - $output .= '
'; + return $output; + } + catch (\Exception $e) { + \Drupal::logger('quant')->error($e->getMessage()); + return ''; } - return $output; } } From f0a86d68d523eb0fa4819563de904dedb19d6e9a Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Sun, 7 Jan 2024 12:48:45 -0800 Subject: [PATCH 2/4] Added todo for issue with missing metadata. --- src/Utility.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Utility.php b/src/Utility.php index 130841b4..5d08cd06 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -146,6 +146,7 @@ public static function getPageInfo(array $urls = NULL) : string { $output .= '
'; $output .= 'Page info for ' . $url . ''; $output .= '
    '; + // @todo Fix underlying data per issue #3412934. $output .= '
  • Published: ' . (($record['meta']['published'] ?? FALSE) ? t('Yes') : t('No')) . '
  • '; $output .= '
  • Revisions: ' . ($record['meta']['revision_count'] ?? 0). '
  • '; $date = DrupalDateTime::createFromTimestamp($record['meta']['content_timestamp'] ?? 0)->format('Y-m-d H:i:s'); From 35a658f0ecd4e2946420da3b29b6d028f909a520 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Sun, 7 Jan 2024 12:51:29 -0800 Subject: [PATCH 3/4] Fixed linting. --- src/Utility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utility.php b/src/Utility.php index 5d08cd06..3124123f 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -148,7 +148,7 @@ public static function getPageInfo(array $urls = NULL) : string { $output .= '
      '; // @todo Fix underlying data per issue #3412934. $output .= '
    • Published: ' . (($record['meta']['published'] ?? FALSE) ? t('Yes') : t('No')) . '
    • '; - $output .= '
    • Revisions: ' . ($record['meta']['revision_count'] ?? 0). '
    • '; + $output .= '
    • Revisions: ' . ($record['meta']['revision_count'] ?? 0) . '
    • '; $date = DrupalDateTime::createFromTimestamp($record['meta']['content_timestamp'] ?? 0)->format('Y-m-d H:i:s'); $output .= '
    • Updated: ' . $date . '
    • '; $date = DrupalDateTime::createFromTimestamp($record['meta']['date_timestamp'] ?? 0)->format('Y-m-d H:i:s'); From 41f22f45c3db9bef0843ac1252ee0c3eb44348c0 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Sun, 7 Jan 2024 12:58:59 -0800 Subject: [PATCH 4/4] Added path alias checks to avoid fatal errors. --- quant.module | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/quant.module b/quant.module index 77f469e9..8f5c9a90 100644 --- a/quant.module +++ b/quant.module @@ -412,6 +412,10 @@ function quant_modules_uninstalled($modules) { * The path alias. */ function quant_path_alias_presave(EntityInterface $pathAlias) { + if (!$pathAlias || !$pathAlias->original) { + return; + } + // Original path alias. $original_path = $pathAlias->original->get('path')->value; $original_alias = $pathAlias->original->get('alias')->value;