Commit 3bf715f3 authored by gbyte.co's avatar gbyte.co

Issue #3043372 by duGuillaume, gbyte.co: Add title and caption to sitemap images

parent c0ce30b1
......@@ -171,30 +171,4 @@ class EntityHelper {
return $entity_query->execute();
}
/**
* Gets a list of image URLs for specified entity ID.
*
* @param string $entity_type_name
* The name of the entity type in which the images are used.
* @param string $entity_id
* The ID of the entity.
*
* @return array
* An array containing the URLs of the images.
*/
public function getEntityImageUrls($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 : [];
}
}
......@@ -162,6 +162,12 @@ class DefaultSitemapGenerator extends SitemapGeneratorBase {
foreach ($link['images'] as $image) {
$this->writer->startElement('image:image');
$this->writer->writeElement('image:loc', $image['path']);
if (strlen($image['title']) > 0) {
$this->writer->writeElement('image:title', $image['title']);
}
if (strlen($image['alt']) > 0) {
$this->writer->writeElement('image:caption', $image['alt']);
}
$this->writer->endElement();
}
}
......
......@@ -122,8 +122,8 @@ class CustomUrlGenerator extends EntityUrlGeneratorBase {
? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($data_set['priority']) ? $data_set['priority'] : NULL,
'changefreq' => !empty($data_set['changefreq']) ? $data_set['changefreq'] : NULL,
'images' => $this->includeImages && method_exists($entity, 'getEntityTypeId')
? $this->getImages($entity->getEntityTypeId(), $entity->id())
'images' => $this->includeImages && !empty($entity)
? $this->getEntityImageData($entity)
: [],
'meta' => [
'path' => $path,
......
......@@ -184,7 +184,7 @@ class EntityMenuLinkContentUrlGenerator extends EntityUrlGeneratorBase {
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => !empty($entity_settings['include_images']) && !empty($entity)
? $this->getImages($entity->getEntityTypeId(), $entity->id())
? $this->getEntityImageData($entity)
: [],
// Additional info useful in hooks.
......
......@@ -165,7 +165,7 @@ class EntityUrlGenerator extends EntityUrlGeneratorBase {
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => !empty($entity_settings['include_images'])
? $this->getImages($entity_type_name, $entity_id)
? $this->getEntityImageData($entity)
: [],
// Additional info useful in hooks.
......
......@@ -5,6 +5,7 @@ namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Simplesitemap;
......@@ -204,15 +205,24 @@ abstract class EntityUrlGeneratorBase extends UrlGeneratorBase {
}
/**
* @param string $entity_type_name
* @param string $entity_id
* @param \Drupal\Core\Entity\ContentEntityBase $entity
*
* @return array
*/
protected function getImages($entity_type_name, $entity_id) {
$images = [];
foreach ($this->entityHelper->getEntityImageUrls($entity_type_name, $entity_id) as $url) {
$images[]['path'] = $this->replaceBaseUrlWithCustom($url);
protected function getEntityImageData(ContentEntityBase $entity) {
$image_data = [];
foreach ($entity->getFieldDefinitions() as $field) {
if ($field->getType() === 'image') {
foreach ($entity->get($field->getName())->getValue() as $value) {
$image_data[] = [
'path' => file_create_url(File::load($value['target_id'])->getFileUri()),
'alt' => $value['alt'],
'title' => $value['title'],
] ;
}
}
}
return $images;
return $image_data;
}
}
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