Skip to content

Commit

Permalink
Merge pull request #213 from quantcdn/feature/page-info-checks
Browse files Browse the repository at this point in the history
Added checks to avoid fatal errors.
  • Loading branch information
kepol authored Jan 8, 2024
2 parents e72e3a4 + 41f22f4 commit 13de090
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
4 changes: 4 additions & 0 deletions quant.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
88 changes: 48 additions & 40 deletions src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,55 +127,63 @@ 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 = '<div class="quant-page-info messages messages--warning">';
$output .= '<h2>' . t('Quant Page Info') . '</h2>';
foreach ($response['global_meta']['records'] as $record) {
$found_urls[] = $url = $record['meta']['url'];
$output .= '<div class="quant-page-info">';
$output .= '<strong>Page info for ' . $url . '</strong>';
$output .= '<ul>';
$output .= '<li><strong>Published</strong>: ' . ($record['meta']['published'] ? t('Yes') : t('No')) . '</li>';
$output .= '<li><strong>Revisions</strong>: ' . $record['meta']['revision_count'] . '</li>';
$date = DrupalDateTime::createFromTimestamp($record['meta']['content_timestamp'])->format('Y-m-d H:i:s');
$output .= '<li><strong>Updated</strong>: ' . $date . '</li>';
$date = DrupalDateTime::createFromTimestamp($record['meta']['date_timestamp'])->format('Y-m-d H:i:s');
$output .= '<li><strong>Synced</strong>: ' . $date . '</li>';
$output .= '</ul>';
$output .= '</div>';
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 .= '<strong>' . t('Page info could not be found for this URL:') . '</strong>';
$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 = '<div class="quant-page-info messages messages--warning">';
$output .= '<h2>' . t('Quant Page Info') . '</h2>';
foreach ($response['global_meta']['records'] as $record) {
$found_urls[] = $url = ($record['meta']['url'] ?? 'Url unknown');
$output .= '<div class="quant-page-info">';
$output .= '<strong>Page info for ' . $url . '</strong>';
$output .= '<ul>';
// @todo Fix underlying data per issue #3412934.
$output .= '<li><strong>Published</strong>: ' . (($record['meta']['published'] ?? FALSE) ? t('Yes') : t('No')) . '</li>';
$output .= '<li><strong>Revisions</strong>: ' . ($record['meta']['revision_count'] ?? 0) . '</li>';
$date = DrupalDateTime::createFromTimestamp($record['meta']['content_timestamp'] ?? 0)->format('Y-m-d H:i:s');
$output .= '<li><strong>Updated</strong>: ' . $date . '</li>';
$date = DrupalDateTime::createFromTimestamp($record['meta']['date_timestamp'] ?? 0)->format('Y-m-d H:i:s');
$output .= '<li><strong>Synced</strong>: ' . $date . '</li>';
$output .= '</ul>';
$output .= '</div>';
}
else {
$output .= '<strong>' . t('Page info could not be found for the following URLs:') . '</strong>';

// Note any URLs that were not in Quant.
if (count($urls) != count($found_urls)) {
if (count($urls) === 1) {
$output .= '<strong>' . t('Page info could not be found for this URL:') . '</strong>';
}
else {
$output .= '<strong>' . t('Page info could not be found for the following URLs:') . '</strong>';
}
$output .= '<ul>';
}
$output .= '<ul>';
}
foreach ($urls as $url) {
if (!in_array($url, $found_urls)) {
$output .= '<li>' . $url . '</li>';
foreach ($urls as $url) {
if (!in_array($url, $found_urls)) {
$output .= '<li>' . $url . '</li>';
}
$output .= '</ul>';
}
$output .= '</ul>';

$output .= '</div>';
}

$output .= '</div>';
return $output;
}
catch (\Exception $e) {
\Drupal::logger('quant')->error($e->getMessage());
return '';
}

return $output;
}

}

0 comments on commit 13de090

Please sign in to comment.