Commit a51f83f0 authored by gbyte.co's avatar gbyte.co

Add code docu to generator service

parent 6056ab57
......@@ -2,10 +2,7 @@
namespace Drupal\simple_sitemap;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManager;
/**
* Simplesitemap class.
......@@ -26,15 +23,13 @@ class Simplesitemap {
* Simplesitemap constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactoryInterface
* The config factory from the container.
*
* @param $database
* The database from the container.
* @param \Drupal\Core\Database\Database $database
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
*/
function __construct(
ConfigFactoryInterface $configFactoryInterface,
\Drupal\Core\Config\ConfigFactoryInterface $configFactoryInterface,
$database,
EntityTypeManager $entityTypeManager) {
\Drupal\Core\Entity\EntityTypeManager $entityTypeManager) {
$this->configFactory = $configFactoryInterface;
$this->db = $database;
......@@ -78,6 +73,7 @@ class Simplesitemap {
return $this;
}
/**
* Enables sitemap support for an entity type. Enabled entity types show
* sitemap settings on their bundles. If an enabled entity type does not
......@@ -120,7 +116,7 @@ class Simplesitemap {
}
/**
* Sets sitemap settings for a bundle less entity type (e.g. user) or a bundle
* Sets sitemap settings for a non-bundle entity type (e.g. user) or a bundle
* of an entity type (e.g. page).
*
* @param string $entity_type_id
......@@ -141,16 +137,14 @@ class Simplesitemap {
return $this;
}
public function getEntityInstanceBundleName($entity) {
return $entity->getEntityTypeId() == 'menu_link_content'
? $entity->getMenuName() : $entity->bundle(); // Menu fix.
}
public function getBundleEntityTypeId($entity) {
return $entity->getEntityTypeId() == 'menu'
? 'menu_link_content' : $entity->getEntityType()->getBundleOf(); // Menu fix.
}
/**
* Overrides entity bundle/entity type sitemap settings for a single entity.
*
* @param $entity_type_id
* @param $id
* @param $settings
* @return $this
*/
public function setEntityInstanceSettings($entity_type_id, $id, $settings) {
$entity_types = $this->getConfig('entity_types');
$entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id);
......@@ -176,6 +170,13 @@ class Simplesitemap {
return $this;
}
/**
* Gets sitemap settings for an entity bundle or a non-bundle entity type.
*
* @param $entity_type_id
* @param null $bundle_name
* @return bool
*/
public function getBundleSettings($entity_type_id, $bundle_name = NULL) {
$bundle_name = is_null($bundle_name) ? $entity_type_id : $bundle_name;
$entity_types = $this->getConfig('entity_types');
......@@ -187,16 +188,38 @@ class Simplesitemap {
return FALSE;
}
/**
* Checks if an entity bundle (or a non-bundle entity type) is set to be
* indexed in the sitemap settings.
*
* @param $entity_type_id
* @param null $bundle_name
* @return bool
*/
public function bundleIsIndexed($entity_type_id, $bundle_name = NULL) {
$settings = $this->getBundleSettings($entity_type_id, $bundle_name);
return !empty($settings['index']);
}
/**
* Checks if an entity type is enabled in the sitemap settings.
*
* @param $entity_type_id
* @return bool
*/
public function entityTypeIsEnabled($entity_type_id) {
$entity_types = $this->getConfig('entity_types');
return isset($entity_types[$entity_type_id]);
}
/**
* Gets sitemap settings for an entity instance which overrides bundle
* settings.
*
* @param $entity_type_id
* @param $id
* @return bool
*/
public function getEntityInstanceSettings($entity_type_id, $id) {
$entity_types = $this->getConfig('entity_types');
$entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id);
......@@ -209,6 +232,13 @@ class Simplesitemap {
}
}
/**
* Adds a custom path to the sitemap settings.
*
* @param $path
* @param $settings
* @return $this|bool
*/
public function addCustomLink($path, $settings) {
if (!\Drupal::service('path.validator')->isValid($path))
return FALSE; // todo: log error
......@@ -228,6 +258,12 @@ class Simplesitemap {
return $this;
}
/**
* Returns settings for a custom path added to the sitemap settings.
*
* @param $path
* @return bool
*/
public function getCustomLink($path) {
$custom_links = $this->getConfig('custom');
foreach($custom_links as $key => $link) {
......@@ -238,6 +274,12 @@ class Simplesitemap {
return FALSE;
}
/**
* Removes a custom path from the sitemap settings.
*
* @param $path
* @return $this
*/
public function removeCustomLink($path) {
$custom_links = $this->getConfig('custom');
foreach($custom_links as $key => $link) {
......@@ -250,6 +292,9 @@ class Simplesitemap {
return $this;
}
/**
* Removes all custom paths from the sitemap settings.
*/
public function removeCustomLinks() {
$this->saveConfig('custom', []);
}
......@@ -271,6 +316,16 @@ class Simplesitemap {
}
}
public function getEntityInstanceBundleName($entity) {
return $entity->getEntityTypeId() == 'menu_link_content'
? $entity->getMenuName() : $entity->bundle(); // Menu fix.
}
public function getBundleEntityTypeId($entity) {
return $entity->getEntityTypeId() == 'menu'
? 'menu_link_content' : $entity->getEntityType()->getBundleOf(); // Menu fix.
}
/**
* Returns the whole sitemap, a requested sitemap chunk,
* or the sitemap index file.
......@@ -330,7 +385,7 @@ class Simplesitemap {
}
/**
* Gets a specific sitemap setting.
* Returns a specific sitemap setting.
*
* @param string $name
* Name of the setting, like 'max_links'.
......
......@@ -5,7 +5,7 @@ namespace Drupal\simple_sitemap\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests Simple XML sitemap integration.
* Tests Simple XML sitemap functional integration.
*
* @group simple_sitemap
*/
......@@ -43,7 +43,7 @@ class SimplesitemapTest extends WebTestBase {
$this->assertRaw('http');
}
public function testGeneration() {
public function testGenerateSitemap() {
// Set up the module.
$this->generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
......@@ -61,7 +61,7 @@ class SimplesitemapTest extends WebTestBase {
/**
* Test overriding of bundle entities.
*/
public function testBundleOverride() {
public function testSetEntityInstanceSettings() {
$this->generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
->setEntityInstanceSettings('node', $this->node->id(), ['index' => 1, 'priority' => '0.1'])
->generateSitemap('nobatch');
......@@ -70,18 +70,6 @@ class SimplesitemapTest extends WebTestBase {
$this->assertText('0.1');
}
/**
* Test sitemap index.
*/
public function testSitemapIndex() {
$this->generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
->saveSetting('max_links', 1)
->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertText('sitemaps/2/sitemap.xml');
}
/**
* Test disabling sitemap support for an entity type.
*/
......@@ -107,6 +95,18 @@ class SimplesitemapTest extends WebTestBase {
$this->assertText('node/');
}
/**
* Test sitemap index.
*/
public function testSitemapIndex() {
$this->generator->setBundleSettings('node', 'page', ['index' => 1, 'priority' => '0.5'])
->saveSetting('max_links', 1)
->generateSitemap('nobatch');
$this->drupalGet('sitemap.xml');
$this->assertText('sitemaps/2/sitemap.xml');
}
/**
* Test adding a custom link to the 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