Commit 2eb800bd authored by andrei.dincu's avatar andrei.dincu

Issue #2249723 by andrei.dincu: Move xmlsitemap_sitemap_load_by_context() to XmlSitemapStorage

parent ede8995a
......@@ -13,6 +13,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\Core\Entity\EntityManagerInterface;
/**
* Returns responses for xmlsitemap.sitemap_xml and xmlsitemap.sitemap_xsl routes.
......@@ -33,15 +34,23 @@ class XmlSitemapController extends ControllerBase {
*/
protected $twig;
/**
* The entity manager object.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs a new XmlSitemapController object.
*
* @param \Drupal\Core\State\StateInterface $state
* The state service.
*/
public function __construct(StateInterface $state, TwigEnvironment $twig) {
public function __construct(StateInterface $state, TwigEnvironment $twig, EntityManagerInterface $entity_manager) {
$this->state = $state;
$this->twig = $twig;
$this->entityManager = $entity_manager;
}
/**
......@@ -49,7 +58,7 @@ class XmlSitemapController extends ControllerBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('state'), $container->get('twig')
$container->get('state'), $container->get('twig'), $container->get('entity.manager')
);
}
......@@ -62,7 +71,7 @@ class XmlSitemapController extends ControllerBase {
* The sitemap in XML format or plain text if xmlsitemap_developer_mode flag is set.
*/
public function renderSitemapXml() {
$sitemap = xmlsitemap_sitemap_load_by_context();
$sitemap = $this->entityManager->getStorage('xmlsitemap')->loadByContext();
if (!$sitemap) {
throw new NotFoundHttpException();
}
......
......@@ -15,17 +15,49 @@ use Drupal\simpletest\WebTestBase;
abstract class XmlSitemapTestBase extends WebTestBase {
public static $modules = array('xmlsitemap');
/**
* The admin user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $admin_user;
/**
* The state store.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The xmlsitemap.settings configuration object.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The entity manager object.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
public function setUp() {
array_unshift(self::$modules, 'xmlsitemap');
parent::setUp();
$this->state = \Drupal::state();
$this->config = \Drupal::configFactory()->get('xmlsitemap.settings');
$this->moduleHandler = \Drupal::moduleHandler();
$this->entityManager = \Drupal::entityManager();
}
public function tearDown() {
......@@ -78,7 +110,7 @@ abstract class XmlSitemapTestBase extends WebTestBase {
* The retrieved HTML string, also available as $this->drupalGetContent()
*/
protected function drupalGetSitemap(array $context = array(), array $options = array(), array $headers = array()) {
$sitemap = xmlsitemap_sitemap_load_by_context($context);
$sitemap = $this->entityManager->getStorage('xmlsitemap')->loadByContext($context);
if (!$sitemap) {
return $this->fail('Could not load sitemap by context.');
}
......
......@@ -202,7 +202,8 @@ function xmlsitemap_modules_disabled(array $modules) {
* Implements hook_robotstxt().
*/
function xmlsitemap_robotstxt() {
if ($sitemap = xmlsitemap_sitemap_load_by_context()) {
$sitemap_storage = \Drupal::entityManager()->getStorage('xmlsitemap');
if ($sitemap = $sitemap_storage->loadByContext()) {
$uri = xmlsitemap_sitemap_uri($sitemap);
$robotstxt[] = 'Sitemap: ' . url($uri['path'], $uri['options']);
return $robotstxt;
......@@ -321,28 +322,6 @@ function xmlsitemap_sitemap_load_multiple($smids = array(), array $conditions =
return $sitemaps;
}
/**
* Load an XML sitemap array from the database based on its context.
*
* @param $context
* An optional XML sitemap context array to use to find the correct XML
* sitemap. If not provided, the current site's context will be used.
*
* @see xmlsitemap_get_current_context()
*/
function xmlsitemap_sitemap_load_by_context(array $context = NULL) {
if (!isset($context)) {
$context = xmlsitemap_get_current_context();
}
$sitemaps = \Drupal::entityManager()->getStorage('xmlsitemap')->loadMultiple();
foreach ($sitemaps as $sitemap) {
if ($sitemap->context == $context) {
return $sitemap;
}
}
return NULL;
}
/**
* Save changes to an XML sitemap or add a new XML sitemap.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment