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 { ...@@ -171,30 +171,4 @@ class EntityHelper {
return $entity_query->execute(); 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 { ...@@ -162,6 +162,12 @@ class DefaultSitemapGenerator extends SitemapGeneratorBase {
foreach ($link['images'] as $image) { foreach ($link['images'] as $image) {
$this->writer->startElement('image:image'); $this->writer->startElement('image:image');
$this->writer->writeElement('image:loc', $image['path']); $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(); $this->writer->endElement();
} }
} }
......
...@@ -122,8 +122,8 @@ class CustomUrlGenerator extends EntityUrlGeneratorBase { ...@@ -122,8 +122,8 @@ class CustomUrlGenerator extends EntityUrlGeneratorBase {
? date_iso8601($entity->getChangedTime()) : NULL, ? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($data_set['priority']) ? $data_set['priority'] : NULL, 'priority' => isset($data_set['priority']) ? $data_set['priority'] : NULL,
'changefreq' => !empty($data_set['changefreq']) ? $data_set['changefreq'] : NULL, 'changefreq' => !empty($data_set['changefreq']) ? $data_set['changefreq'] : NULL,
'images' => $this->includeImages && method_exists($entity, 'getEntityTypeId') 'images' => $this->includeImages && !empty($entity)
? $this->getImages($entity->getEntityTypeId(), $entity->id()) ? $this->getEntityImageData($entity)
: [], : [],
'meta' => [ 'meta' => [
'path' => $path, 'path' => $path,
......
...@@ -184,7 +184,7 @@ class EntityMenuLinkContentUrlGenerator extends EntityUrlGeneratorBase { ...@@ -184,7 +184,7 @@ class EntityMenuLinkContentUrlGenerator extends EntityUrlGeneratorBase {
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL, 'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL, 'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => !empty($entity_settings['include_images']) && !empty($entity) 'images' => !empty($entity_settings['include_images']) && !empty($entity)
? $this->getImages($entity->getEntityTypeId(), $entity->id()) ? $this->getEntityImageData($entity)
: [], : [],
// Additional info useful in hooks. // Additional info useful in hooks.
......
...@@ -165,7 +165,7 @@ class EntityUrlGenerator extends EntityUrlGeneratorBase { ...@@ -165,7 +165,7 @@ class EntityUrlGenerator extends EntityUrlGeneratorBase {
'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL, 'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL, 'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
'images' => !empty($entity_settings['include_images']) 'images' => !empty($entity_settings['include_images'])
? $this->getImages($entity_type_name, $entity_id) ? $this->getEntityImageData($entity)
: [], : [],
// Additional info useful in hooks. // Additional info useful in hooks.
......
...@@ -5,6 +5,7 @@ namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator; ...@@ -5,6 +5,7 @@ namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\simple_sitemap\EntityHelper; use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger; use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Simplesitemap; use Drupal\simple_sitemap\Simplesitemap;
...@@ -204,15 +205,24 @@ abstract class EntityUrlGeneratorBase extends UrlGeneratorBase { ...@@ -204,15 +205,24 @@ abstract class EntityUrlGeneratorBase extends UrlGeneratorBase {
} }
/** /**
* @param string $entity_type_name * @param \Drupal\Core\Entity\ContentEntityBase $entity
* @param string $entity_id *
* @return array * @return array
*/ */
protected function getImages($entity_type_name, $entity_id) { protected function getEntityImageData(ContentEntityBase $entity) {
$images = []; $image_data = [];
foreach ($this->entityHelper->getEntityImageUrls($entity_type_name, $entity_id) as $url) { foreach ($entity->getFieldDefinitions() as $field) {
$images[]['path'] = $this->replaceBaseUrlWithCustom($url); 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