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

git commit -m 'Issue #2903165 by MDJ Webdiensten, zenimagine: Add images to sitemap'

parent 1a006b3c
......@@ -41,6 +41,9 @@ simple_sitemap.bundle_settings.*.*:
changefreq:
label: 'Change frequency'
type: string
include_images:
label: 'Include images'
type: 'integer'
simple_sitemap.custom:
label: 'Custom links'
......
......@@ -12,8 +12,9 @@
var vals = [];
if ($(context).find('#edit-simple-sitemap-index-content-1').is(':checked')) {
vals.push(Drupal.t('Included in sitemap'));
vals.push(Drupal.t('Priority') + ' ' + $('#edit-simple-sitemap-priority option:selected', context).text());
vals.push(Drupal.t('Change frequency') + ' ' + $('#edit-simple-sitemap-changefreq option:selected', context).text());
vals.push(Drupal.t('Priority') + ': ' + $('#edit-simple-sitemap-priority option:selected', context).text());
vals.push(Drupal.t('Change frequency') + ': ' + $('#edit-simple-sitemap-changefreq option:selected', context).text());
vals.push(Drupal.t('Include images') + ': ' + $('#edit-simple-sitemap-include-images option:selected', context).text());
}
else {
vals.push(Drupal.t('Excluded from sitemap'));
......
......@@ -16,10 +16,12 @@
if ($('#edit-simple-sitemap-index-content-1').is(':checked')) {
$('.form-item-simple-sitemap-priority').show();
$('.form-item-simple-sitemap-changefreq').show();
$('.form-item-simple-sitemap-include-images').show();
}
else {
$('.form-item-simple-sitemap-priority').hide();
$('.form-item-simple-sitemap-changefreq').hide();
$('.form-item-simple-sitemap-include-images').hide();
}
// On change: Show or hide settings dependant on 'enabled' setting.
......@@ -27,17 +29,19 @@
if ($('#edit-simple-sitemap-index-content-1').is(':checked')) {
$('.form-item-simple-sitemap-priority').show();
$('.form-item-simple-sitemap-changefreq').show();
$('.form-item-simple-sitemap-include-images').show();
}
else {
$('.form-item-simple-sitemap-priority').hide();
$('.form-item-simple-sitemap-changefreq').hide();
$('.form-item-simple-sitemap-include-images').hide();
}
// Show 'Regenerate sitemap' field if 'enabled' setting has changed.
$('.form-item-simple-sitemap-regenerate-now').show();
});
// Show 'Regenerate sitemap' field if settings have changed.
$("#edit-simple-sitemap-priority, #edit-simple-sitemap-changefreq").change(function() {
$("#edit-simple-sitemap-priority, #edit-simple-sitemap-changefreq, #edit-simple-sitemap-include-images").change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
});
}
......
......@@ -39,15 +39,18 @@
var enabledId = '#edit-' + value + '-enabled';
var priorityId = '.form-item-' + value + '-simple-sitemap-priority';
var changefreqId = '.form-item-' + value + '-simple-sitemap-changefreq';
var includeImagesId = '.form-item-' + value + '-simple-sitemap-include-images';
// On load: Show or hide settings dependent on 'enabled' checkbox.
if ($(enabledId).is(':checked')) {
$(priorityId).show();
$(changefreqId).show();
$(includeImagesId).show();
}
else {
$(priorityId).hide();
$(changefreqId).hide();
$(includeImagesId).hide();
}
// On change: Show or hide settings dependent on 'enabled' checkbox.
......@@ -55,15 +58,17 @@
if ($(enabledId).is(':checked')) {
$(priorityId).show();
$(changefreqId).show();
$(includeImagesId).show();
}
else {
$(priorityId).hide();
$(changefreqId).hide();
$(includeImagesId).hide();
}
});
// Show 'Regenerate sitemap' field if settings have changed.
$(priorityId, changefreqId).change(function() {
$(priorityId, changefreqId, includeImagesId).change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
});
});
......
......@@ -7,6 +7,9 @@
/**
* Implements hook_requirements().
*
* @param $phase
* @return array
*/
function simple_sitemap_requirements($phase) {
$requirements = [];
......@@ -325,7 +328,7 @@ function simple_sitemap_update_8207() {
}
/**
* Adding changefreq setting to all existing bundle and custom link settings.
* Adding changefreq setting to all existing bundle and entity instance settings.
*/
function simple_sitemap_update_8208() {
......@@ -356,4 +359,42 @@ function simple_sitemap_update_8208() {
->execute();
}
}
return t('You may now want to configure the new changefreq setting for sitemap entities and custom links.');
}
/**
* Adding image inclusion setting to all existing bundle and entity instance settings.
*/
function simple_sitemap_update_8209() {
// Update existing bundle settings.
$config_factory = \Drupal::service('config.factory');
$entity_types = $config_factory->listAll('simple_sitemap.bundle_settings.');
foreach ($entity_types as $entity_type) {
$config = $config_factory->get($entity_type)->get();
if (!isset($config['include_images'])) {
$config_factory->getEditable($entity_type)
->setData($config + ['include_images' => 0])
->save();
}
}
// Update existing entity override data.
$results = \Drupal::database()->select('simple_sitemap_entity_overrides', 'o')
->fields('o', ['id', 'inclusion_settings'])
->execute()->fetchAll(\PDO::FETCH_OBJ);
foreach ($results as $row) {
$settings = unserialize($row->inclusion_settings);
if (!isset($settings['include_images'])) {
\Drupal::database()->update('simple_sitemap_entity_overrides')
->fields(['inclusion_settings' => serialize($settings + ['include_images' => 0]),])
->condition('id', $row->id)
->execute();
}
}
return t('You may now want to configure your sitemap entities to include images.');
}
......@@ -112,6 +112,7 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
'index' => $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority'],
'changefreq' => $values['simple_sitemap_changefreq'],
'include_images' => $values['simple_sitemap_include_images'],
];
switch ($f->getEntityCategory()) {
......
......@@ -26,6 +26,7 @@ services:
public: true
arguments:
- '@entity_type.manager'
- '@database'
simple_sitemap.form_helper:
class: Drupal\simple_sitemap\Form\FormHelper
......@@ -49,6 +50,7 @@ services:
- '@entity_type.manager'
- '@path.validator'
- '@simple_sitemap.logger'
- '@simple_sitemap.entity_helper'
simple_sitemap.custom_url_generator:
class: Drupal\simple_sitemap\Batch\CustomUrlGenerator
......@@ -60,6 +62,7 @@ services:
- '@entity_type.manager'
- '@path.validator'
- '@simple_sitemap.logger'
- '@simple_sitemap.entity_helper'
simple_sitemap.logger:
class: Drupal\simple_sitemap\Logger
......
......@@ -39,7 +39,7 @@ class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
continue;
}
$entity = $this->getEntityFromUrlObject($url_object);
$entity = $this->entityHelper->getEntityFromUrlObject($url_object);
$path_data = [
'path' => $path,
......@@ -75,17 +75,4 @@ class CustomUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
return $custom_paths;
}
/**
* @param $url_object
* @return object|null
*/
protected function getEntityFromUrlObject(Url $url_object) {
$route_parameters = $url_object->getRouteParameters();
return !empty($route_parameters) && $this->entityTypeManager
->getDefinition($entity_type_id = key($route_parameters), FALSE)
? $this->entityTypeManager->getStorage($entity_type_id)
->load($route_parameters[$entity_type_id])
: NULL;
}
}
......@@ -63,7 +63,11 @@ class EntityUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa
? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => $entity_settings['include_images']
? $this->entityHelper->getEntityImageUris($entity_info['entity_type_name'], $entity_id)
: []
];
$this->addUrlVariants($url_object, $path_data, $entity);
}
$this->processSegment();
......
......@@ -7,6 +7,7 @@ use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\Entity;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SitemapGenerator;
......@@ -83,13 +84,19 @@ class UrlGeneratorBase {
protected $batchInfo;
/**
* BatchUrlGenerator constructor.
* @var \Drupal\simple_sitemap\EntityHelper
*/
protected $entityHelper;
/**
* UrlGeneratorBase constructor.
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param \Drupal\simple_sitemap\SitemapGenerator $sitemap_generator
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* @param \Drupal\Core\Path\PathValidator $path_validator
* @param \Drupal\simple_sitemap\Logger $logger
* @param \Drupal\simple_sitemap\EntityHelper $entityHelper
*/
public function __construct(
Simplesitemap $generator,
......@@ -97,7 +104,8 @@ class UrlGeneratorBase {
LanguageManagerInterface $language_manager,
EntityTypeManagerInterface $entity_type_manager,
PathValidator $path_validator,
Logger $logger
Logger $logger,
EntityHelper $entityHelper
) {
$this->generator = $generator;
$this->sitemapGenerator = $sitemap_generator; // todo: using only one method, maybe make method static instead?
......@@ -107,6 +115,7 @@ class UrlGeneratorBase {
$this->entityTypeManager = $entity_type_manager;
$this->pathValidator = $path_validator;
$this->logger = $logger;
$this->entityHelper = $entityHelper;
$this->anonUser = $this->entityTypeManager->getStorage('user')
->load(self::ANONYMOUS_USER_ID);
}
......
......@@ -4,7 +4,9 @@ namespace Drupal\simple_sitemap;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Url;
/**
* Class EntityHelper
......@@ -17,12 +19,19 @@ class EntityHelper {
*/
protected $entityTypeManager;
/**
* @var \Drupal\Core\Database\Connection
*/
protected $db;
/**
* EntityHelper constructor.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Database\Connection $database
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database) {
$this->entityTypeManager = $entityTypeManager;
$this->db = $database;
}
/**
......@@ -91,4 +100,37 @@ class EntityHelper {
return FALSE;
}
/**
* @param $url_object
* @return object|null
*/
public function getEntityFromUrlObject(Url $url_object) {
$route_parameters = $url_object->getRouteParameters();
return !empty($route_parameters) && $this->entityTypeManager
->getDefinition($entity_type_id = key($route_parameters), FALSE)
? $this->entityTypeManager->getStorage($entity_type_id)
->load($route_parameters[$entity_type_id])
: NULL;
}
/**
* @param $entity_type_name
* @param $entity_id
* @return array
*/
public function getEntityImageUris($entity_type_name, $entity_id) {
$query = $this->db->select('file_managed', 'fm');
$query->fields('fm', ['uri']);
$query->join('file_usage', 'fu', 'fu.fid = fm.fid');
$query->condition('fm.filemime', 'image/%', 'LIKE');
$query->condition('fu.type', $entity_type_name);
$query->condition('fu.id', $entity_id);
foreach ($query->execute() as $row) {
$imageUris[] = file_create_url($row->uri);
}
return !empty($imageUris) ? $imageUris : [];
}
}
......@@ -80,6 +80,7 @@ class FormHelper {
'simple_sitemap_index_content',
'simple_sitemap_priority',
'simple_sitemap_changefreq',
'simple_sitemap_include_images',
'simple_sitemap_regenerate_now',
];
......@@ -227,7 +228,7 @@ class FormHelper {
public function displayEntitySettings(&$form_fragment, $multiple = FALSE) {
$prefix = $multiple ? $this->getEntityTypeId() . '_' : '';
if ($this->getEntityCategory() == 'instance') {
if ($this->getEntityCategory() === 'instance') {
$bundle_settings = $this->generator->getBundleSettings($this->getEntityTypeId(), $this->getBundleName());
$settings = NULL !== $this->getInstanceId() ? $this->generator->getEntityInstanceSettings($this->getEntityTypeId(), $this->getInstanceId()) : $bundle_settings;
}
......@@ -237,6 +238,7 @@ class FormHelper {
$index = isset($settings['index']) ? $settings['index'] : 0;
$priority = isset($settings['priority']) ? $settings['priority'] : self::PRIORITY_DEFAULT;
$changefreq = !empty($settings['changefreq']) ? $settings['changefreq'] : '';
$include_images = !empty($settings['include_images']) ? $settings['include_images'] : 0;
$bundle_name = !empty($this->getBundleName()) ? $this->getBundleName() : $this->t('undefined');
if (!$multiple) {
......@@ -244,46 +246,55 @@ class FormHelper {
'#type' => 'radios',
'#default_value' => $index,
'#options' => [
0 => $this->getEntityCategory() == 'instance' ? $this->t('Do not index this @bundle entity', ['@bundle' => $bundle_name]) : $this->t('Do not index entities of this type'),
1 => $this->getEntityCategory() == 'instance' ? $this->t('Index this @bundle entity', ['@bundle' => $bundle_name]) : $this->t('Index entities of this type'),
0 => $this->getEntityCategory() === 'instance' ? $this->t('Do not index this @bundle entity', ['@bundle' => $bundle_name]) : $this->t('Do not index entities of this type'),
1 => $this->getEntityCategory() === 'instance' ? $this->t('Index this @bundle entity', ['@bundle' => $bundle_name]) : $this->t('Index entities of this type'),
],
];
if ($this->getEntityCategory() == 'instance' && isset($bundle_settings['index'])) {
$form_fragment[$prefix . 'simple_sitemap_index_content']['#options'][$bundle_settings['index']] .= ' <em>(' . $this->t('Default') . ')</em>';
if ($this->getEntityCategory() === 'instance' && isset($bundle_settings['index'])) {
$form_fragment[$prefix . 'simple_sitemap_index_content']['#options'][$bundle_settings['index']] .= ' <em>(' . $this->t('default') . ')</em>';
}
}
if ($this->getEntityCategory() == 'instance') {
$priority_description = $this->t('The priority this @bundle entity will have in the eyes of search engine bots.', ['@bundle' => $bundle_name]);
$changefreq_description = $this->t('The frequency with which this @bundle entity changes. Search engine bots may take this as an indication of how often to index it.', ['@bundle' => $bundle_name]);
}
else {
$priority_description = $this->t('The priority entities of this type will have in the eyes of search engine bots.');
$changefreq_description = $this->t('The frequency with which entities of this type change. Search engine bots may take this as an indication of how often to index them.');
}
$form_fragment[$prefix . 'simple_sitemap_priority'] = [
'#type' => 'select',
'#title' => $this->t('Priority'),
'#description' => $priority_description,
'#description' => $this->getEntityCategory() === 'instance'
? $this->t('The priority this @bundle entity will have in the eyes of search engine bots.', ['@bundle' => $bundle_name])
: $this->t('The priority entities of this type will have in the eyes of search engine bots.'),
'#default_value' => $priority,
'#options' => $this->getPrioritySelectValues(),
];
if ($this->getEntityCategory() == 'instance' && isset($bundle_settings['priority'])) {
$form_fragment[$prefix . 'simple_sitemap_priority']['#options'][$this->formatPriority($bundle_settings['priority'])] .= ' (' . $this->t('Default') . ')';
if ($this->getEntityCategory() === 'instance' && isset($bundle_settings['priority'])) {
$form_fragment[$prefix . 'simple_sitemap_priority']['#options'][$this->formatPriority($bundle_settings['priority'])] .= ' (' . $this->t('default') . ')';
}
$form_fragment[$prefix . 'simple_sitemap_changefreq'] = [
'#type' => 'select',
'#title' => $this->t('Change frequency'),
'#description' => $changefreq_description,
'#description' => $this->getEntityCategory() === 'instance'
? $this->t('The frequency with which this @bundle entity changes. Search engine bots may take this as an indication of how often to index it.', ['@bundle' => $bundle_name])
: $this->t('The frequency with which entities of this type change. Search engine bots may take this as an indication of how often to index them.'),
'#default_value' => $changefreq,
'#options' => $this->getChangefreqSelectValues(),
];
if ($this->getEntityCategory() == 'instance' && isset($bundle_settings['changefreq'])) {
$form_fragment[$prefix . 'simple_sitemap_changefreq']['#options'][$bundle_settings['changefreq']] .= ' (' . $this->t('Default') . ')';
if ($this->getEntityCategory() === 'instance' && isset($bundle_settings['changefreq'])) {
$form_fragment[$prefix . 'simple_sitemap_changefreq']['#options'][$bundle_settings['changefreq']] .= ' (' . $this->t('default') . ')';
}
$form_fragment[$prefix . 'simple_sitemap_include_images'] = [
'#type' => 'select',
'#title' => $this->t('Include images'),
'#description' => $this->getEntityCategory() === 'instance'
? $this->t('Determines if images referenced by this @bundle entity should be included into the sitemap.', ['@bundle' => $bundle_name])
: $this->t('Determines if images referenced by entities of this type should be included into the sitemap.'),
'#default_value' => $include_images,
'#options' => [0 => $this->t('No'), 1 => $this->t('Yes')],
];
if ($this->getEntityCategory() === 'instance' && isset($bundle_settings['include_images'])) {
$form_fragment[$prefix . 'simple_sitemap_include_images']['#options'][$bundle_settings['include_images']] .= ' (' . $this->t('default') . ')';
}
return $this;
......
......@@ -97,6 +97,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
'index' => TRUE,
'priority' => $values[$entity_type_id . '_simple_sitemap_priority'],
'changefreq' => $values[$entity_type_id . '_simple_sitemap_changefreq'],
'include_images' => $values[$entity_type_id . '_simple_sitemap_include_images'],
]);
}
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\simple_sitemap;
use Drupal\simple_sitemap\Form\FormHelper;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidator;
......@@ -54,7 +53,7 @@ class Simplesitemap {
* @var array
*/
protected static $allowed_link_settings = [
'entity' => ['index', 'priority', 'changefreq'],
'entity' => ['index', 'priority', 'changefreq', 'include_images'],
'custom' => ['priority', 'changefreq'],
];
......@@ -62,6 +61,7 @@ class Simplesitemap {
'index' => 1,
'priority' => 0.5,
'changefreq' => '',
'include_images' => 0,
];
/**
......@@ -295,7 +295,7 @@ class Simplesitemap {
* Name of the bundle. NULL if entity type has no bundles.
* @param array $settings
* An array of sitemap settings for this bundle/entity type.
* Example: ['index' => TRUE, 'priority' => 0.5, 'changefreq' => 'never'].
* Example: ['index' => TRUE, 'priority' => 0.5, 'changefreq' => 'never', 'include_images' => FALSE].
*
* @return $this
*
......
......@@ -20,6 +20,7 @@ class SitemapGenerator {
const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
const GENERATED_BY = 'Generated by the Simple XML sitemap Drupal module: https://drupal.org/project/simple_sitemap.';
const FIRST_CHUNK_INDEX = 1;
const XMLNS_IMAGE = 'http://www.google.com/schemas/sitemap-image/1.1';
/**
* @var \Drupal\simple_sitemap\Batch\Batch
......@@ -230,6 +231,7 @@ class SitemapGenerator {
$writer->writeComment(self::GENERATED_BY);
$writer->startElement('sitemapindex');
$writer->writeAttribute('xmlns', self::XMLNS);
$writer->writeAttribute('xmlns:image', self::XMLNS_IMAGE);
foreach ($chunk_info as $chunk_id => $chunk_data) {
$writer->startElement('sitemap');
......@@ -264,6 +266,7 @@ class SitemapGenerator {
$writer->writeComment(self::GENERATED_BY);
$writer->startElement('urlset');
$writer->writeAttribute('xmlns', self::XMLNS);
$writer->writeAttribute('xmlns:image', self::XMLNS_IMAGE);
if ($this->isHreflangSitemap()) {
$writer->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);
......@@ -303,6 +306,15 @@ class SitemapGenerator {
$writer->writeElement('priority', $link['priority']);
}
// Add images if any.
if (!empty($link['images'])) {
foreach ($link['images'] as $image_url) {
$writer->startElement('image:image');
$writer->writeElement('image:loc', $image_url);
$writer->endElement();
}
}
$writer->endElement();
}
$writer->endElement();
......
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