Commit afc20a1b authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #2941322 by webflo, gbyte.co: Expose sitemap entity settings in views

parent 7ad3960b
......@@ -10,6 +10,10 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\system\MenuInterface;
use Drupal\language\ConfigurableLanguageInterface;
use Drupal\simple_sitemap\Plugin\Field\SimplesitemapIndexItem;
use Drupal\simple_sitemap\Plugin\Field\SimplesitemapPriorityItem;
use Drupal\simple_sitemap\Plugin\Field\SimplesitemapChangefreqItem;
use Drupal\simple_sitemap\Plugin\Field\SimplesitemapIncludeImagesItem;
/**
*Implements hook_help.
......@@ -246,3 +250,48 @@ function simple_sitemap_robotstxt() {
'Sitemap: ' . $GLOBALS['base_url'] . '/sitemap.xml',
];
}
/**
* Implements hook_entity_base_field_info().
*/
function simple_sitemap_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
/** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $entity_types */
$entity_types = \Drupal::service('simple_sitemap.entity_helper')->getSupportedEntityTypes();
$fields = [];
if (isset($entity_types[$entity_type->id()])) {
$definitions = [
'simple_sitemap_index' => [
'type' => 'boolean',
'name' => 'Indexed in sitemap',
'class' => SimplesitemapIndexItem::class,
],
'simple_sitemap_priority' => [
'type' => 'decimal',
'name' => 'Priority in sitemap',
'class' => SimplesitemapPriorityItem::class,
],
'simple_sitemap_changefreq' => [
'type' => 'string',
'name' => 'Change frequency in sitemap',
'class' => SimplesitemapChangefreqItem::class,
],
'simple_sitemap_include_images' => [
'type' => 'boolean',
'name' => 'Include images in sitemap',
'class' => SimplesitemapIncludeImagesItem::class,
],
];
foreach ($definitions as $id => $definition) {
$fields[$id] = \Drupal\Core\Field\BaseFieldDefinition::create($definition['type'])
->setCardinality(1)
->setName($definition['name'])
->setComputed(TRUE)
->setClass($definition['class']);
}
}
return $fields;
}
<?php
/**
* @file
* Provide views data for simple_sitemap.module.
*/
/**
* Implements hook_views_data().
*/
function simple_sitemap_views_data() {
/** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $entity_types */
$entity_types = \Drupal::service('simple_sitemap.entity_helper')->getSupportedEntityTypes();
$data = [];
foreach ($entity_types as $entity_type) {
$table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
$data[$table]['simple_sitemap_index'] = [
'title' => t('Indexed in sitemap'),
'field' => [
'id' => 'field',
'default_formatter' => 'boolean',
'field_name' => 'simple_sitemap_index',
]
];
$data[$table]['simple_sitemap_priority'] = [
'title' => t('Priority in sitemap'),
'field' => [
'id' => 'field',
'default_formatter' => 'number_decimal',
'field_name' => 'simple_sitemap_priority',
]
];
$data[$table]['simple_sitemap_changefreq'] = [
'title' => t('Change frequency in sitemap'),
'field' => [
'id' => 'field',
'default_formatter' => 'string',
'field_name' => 'simple_sitemap_changefreq',
]
];
$data[$table]['simple_sitemap_include_images'] = [
'title' => t('Include images in sitemap'),
'field' => [
'id' => 'field',
'default_formatter' => 'boolean',
'field_name' => 'simple_sitemap_include_images',
]
];
}
return $data;
}
<?php
namespace Drupal\simple_sitemap\Plugin\Field;
class SimplesitemapChangefreqItem extends SimplesitemapItemBase {
protected $settingName = 'changefreq';
protected $defaultValue = '';
}
<?php
namespace Drupal\simple_sitemap\Plugin\Field;
class SimplesitemapIncludeImagesItem extends SimplesitemapItemBase {
protected $settingName = 'include_images';
protected $defaultValue = FALSE;
}
<?php
namespace Drupal\simple_sitemap\Plugin\Field;
class SimplesitemapIndexItem extends SimplesitemapItemBase {
protected $settingName = 'index';
protected $defaultValue = FALSE;
}
<?php
namespace Drupal\simple_sitemap\Plugin\Field;
use Drupal\Core\Field\FieldItemList;
abstract class SimplesitemapItemBase extends FieldItemList {
protected $settingName;
protected $defaultValue;
function computedListProperty() {
$entity = $this->getEntity();
$generator = \Drupal::service('simple_sitemap.generator');
if ($generator->entityTypeIsEnabled($entity->getEntityTypeId())) {
if (!empty($entity->id())) {
$settings = $generator->getEntityInstanceSettings(
$entity->getEntityTypeId(),
$entity->id()
);
}
elseif (!empty($entity->getEntityTypeId())) {
$settings = $generator->getBundleSettings(
$entity->getEntityTypeId(),
$entity->bundle()
);
}
$this->list[0] = $this->createItem(
0,
isset($settings[$this->settingName]) ? $settings[$this->settingName] : $this->defaultValue
);
}
}
/**
* {@inheritdoc}
*/
public function get($index) {
$this->computedListProperty();
return isset($this->list[$index]) ? $this->list[$index] : NULL;
}
/**
* {@inheritdoc}
*/
public function getIterator() {
$this->computedListProperty();
return parent::getIterator();
}
}
<?php
namespace Drupal\simple_sitemap\Plugin\Field;
class SimplesitemapPriorityItem extends SimplesitemapItemBase {
protected $settingName = 'priority';
protected $defaultValue = 0.5;
}
......@@ -622,6 +622,8 @@ class Simplesitemap {
* Array of sitemap settings for an entity bundle, a non-bundle entity type
* or for all entity types and their bundles.
* False if entity type does not exist.
*
* @todo May want to return default settings in case bundle of enabled entity type does not have its bundle_settings.*.* configuration file.
*/
public function getBundleSettings($entity_type_id = NULL, $bundle_name = NULL) {
if (NULL !== $entity_type_id) {
......
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