/** * Renders a list with all custom links. * * @return array * The list to be rendered. */ public function render() { $build['xmlsitemap_add_custom'] = array('#type' => 'link', '#title' => t('Add custom link'), '#href' => 'admin/config/search/xmlsitemap/custom/add'); $header = array('loc' => array('data' => t('Location'), 'field' => 'loc', 'sort' => 'asc'), 'priority' => array('data' => t('Priority'), 'field' => 'priority'), 'changefreq' => array('data' => t('Change frequency'), 'field' => 'changefreq'), 'language' => array('data' => t('Language'), 'field' => 'language'), 'operations' => array('data' => t('Operations'))); $rows = array(); $destination = drupal_get_destination(); $query = db_select('xmlsitemap'); $query->fields('xmlsitemap'); $query->condition('type', 'custom'); $query->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')->limit(50); $query->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')->orderByHeader($header); $result = $query->execute(); foreach ($result as $link) { $language = $this->languageManager->getLanguage($link->language); $row = array(); $row['loc'] = $this->l($link->loc, Url::fromUri($link->loc)); $row['priority'] = number_format($link->priority, 1); $row['changefreq'] = $link->changefreq ? drupal_ucfirst(xmlsitemap_get_changefreq($link->changefreq)) : t('None'); if (isset($header['language'])) { $row['language'] = t($language->name); } $operations['edit'] = array('title' => t('Edit'), 'route_name' => 'xmlsitemap_custom.edit', 'route_parameters' => array('link' => $link->id)); $operations['delete'] = array('title' => t('Delete'), 'route_name' => 'xmlsitemap_custom.delete', 'route_parameters' => array('link' => $link->id)); $row['operations'] = array('data' => array('#type' => 'operations', '#theme' => 'links', '#links' => $operations, '#attributes' => array('class' => array('links', 'inline')))); $rows[] = $row; } // @todo Convert to tableselect $build['xmlsitemap_custom_table'] = array('#type' => 'tableselect', '#theme' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => $this->t('No custom links available. <a href="@custom_link">Add custom link</a>', array('@custom_link' => Url::fromRoute('xmlsitemap_custom.add', [], array('query' => $destination))))); $build['xmlsitemap_custom_pager'] = array('#theme' => 'pager'); return $build; }
/** * Tests for xmlsitemap_get_changefreq(). */ public function testGetChangefreq() { // The test values. $values = array(0, mt_rand(1, XMLSITEMAP_FREQUENCY_ALWAYS), mt_rand(XMLSITEMAP_FREQUENCY_ALWAYS + 1, XMLSITEMAP_FREQUENCY_HOURLY), mt_rand(XMLSITEMAP_FREQUENCY_HOURLY + 1, XMLSITEMAP_FREQUENCY_DAILY), mt_rand(XMLSITEMAP_FREQUENCY_DAILY + 1, XMLSITEMAP_FREQUENCY_WEEKLY), mt_rand(XMLSITEMAP_FREQUENCY_WEEKLY + 1, XMLSITEMAP_FREQUENCY_MONTHLY), mt_rand(XMLSITEMAP_FREQUENCY_MONTHLY + 1, XMLSITEMAP_FREQUENCY_YEARLY), mt_rand(XMLSITEMAP_FREQUENCY_YEARLY + 1, mt_getrandmax())); // The expected values. $expected = array(FALSE, 'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'); foreach ($values as $i => $value) { $actual = xmlsitemap_get_changefreq($value); $this->assertIdentical($actual, $expected[$i]); } }
/** * {@inheritdoc} */ public function generateChunk(XmlSitemapInterface $sitemap, XmlSitemapWriter $writer, $chunk) { $lastmod_format = $this->config->get('lastmod_format'); $url_options = $sitemap->uri['options']; $url_options += array('absolute' => TRUE, 'xmlsitemap_base_url' => $this->state->get('xmlsitemap_base_url'), 'language' => $this->languageManager->getDefaultLanguage(), 'alias' => $this->config->get('prefetch_aliases')); $last_url = ''; $link_count = 0; $query = db_select('xmlsitemap', 'x'); $query->fields('x', array('loc', 'lastmod', 'changefreq', 'changecount', 'priority', 'language', 'access', 'status')); $query->condition('x.access', 1); $query->condition('x.status', 1); $query->orderBy('x.language', 'DESC'); $query->orderBy('x.loc'); $query->addTag('xmlsitemap_generate'); $query->addMetaData('sitemap', $sitemap); $offset = max($chunk - 1, 0) * xmlsitemap_get_chunk_size(); $limit = xmlsitemap_get_chunk_size(); $query->range($offset, $limit); $links = $query->execute(); while ($link = $links->fetchAssoc()) { $link['language'] = $link['language'] != LanguageInterface::LANGCODE_NOT_SPECIFIED ? xmlsitemap_language_load($link['language']) : $url_options['language']; if ($url_options['alias']) { $link['loc'] = $this->getPathAlias($link['loc'], $link['language']->getId()); } if ($url_options['base_url']) { $link['loc'] = rtrim($url_options['base_url'], '/') . '/' . ltrim($link['loc'], '/'); } $link_options = array('language' => $link['language'], 'xmlsitemap_link' => $link, 'xmlsitemap_sitemap' => $sitemap); // @todo Add a separate hook_xmlsitemap_link_url_alter() here? $link['loc'] = empty($link['loc']) ? '<front>' : $link['loc']; $link_url = Url::fromUri($link['loc'], [], $link_options + $url_options)->toString(); // Skip this link if it was a duplicate of the last one. // @todo Figure out a way to do this before generation so we can report // back to the user about this. if ($link_url == $last_url) { continue; } else { $last_url = $link_url; // Keep track of the total number of links written. $link_count++; } $element = array(); $element['loc'] = $link_url; if ($link['lastmod']) { $element['lastmod'] = gmdate($lastmod_format, $link['lastmod']); // If the link has a lastmod value, update the changefreq so that links // with a short changefreq but updated two years ago show decay. // We use abs() here just incase items were created on this same cron // run because lastmod would be greater than REQUEST_TIME. $link['changefreq'] = (abs(REQUEST_TIME - $link['lastmod']) + $link['changefreq']) / 2; } if ($link['changefreq']) { $element['changefreq'] = xmlsitemap_get_changefreq($link['changefreq']); } if (isset($link['priority']) && $link['priority'] != 0.5) { // Don't output the priority value for links that have 0.5 priority. // This is the default 'assumed' value if priority is not included as // per the sitemaps.org specification. $element['priority'] = number_format($link['priority'], 1); } $writer->writeSitemapElement('url', $element); } return $link_count; }