class MediaSitemapGenerator in Media Sitemap 8
Class MediaSitemapGenerator.
@package Drupal\media_sitemap
Hierarchy
- class \Drupal\media_sitemap\MediaSitemapGenerator implements MediaSitemapGeneratorInterface
Expanded class hierarchy of MediaSitemapGenerator
2 files declare their use of MediaSitemapGenerator
- MediaSitemapBatchController.php in src/
Controller/ MediaSitemapBatchController.php - Contains \Drupal\media_sitemap\Controller\MediaSitemapBatchController.
- media_sitemap.module in ./
media_sitemap.module - Creating image sitemap for Google using media module.
1 string reference to 'MediaSitemapGenerator'
1 service uses MediaSitemapGenerator
File
- src/
MediaSitemapGenerator.php, line 22 - Contains \Drupal\media_sitemap\MediaSitemapGenerator.
Namespace
Drupal\media_sitemapView source
class MediaSitemapGenerator implements MediaSitemapGeneratorInterface {
/**
* @var \Drupal\Core\Database\Driver\pgsql\Connection
*/
protected $database;
/**
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* @var \Drupal\Core\File\FileSystem
*/
protected $fileSystem;
/**
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* MediaSitemapGenerator constructor.
* @param Connection $database
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* @param \Drupal\Core\File\FileSystem $fileSystem
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
*/
public function __construct(Connection $database, ConfigFactory $configFactory, FileSystem $fileSystem, MessengerInterface $messenger) {
$this->database = $database;
$this->configFactory = $configFactory;
$this->fileSystem = $fileSystem;
$this->messenger = $messenger;
}
public static function generateSitemap() {
$database = \Drupal::service('database');
$configFactory = \Drupal::service('config.factory');
$fileSystem = \Drupal::service('file_system');
$messenger = \Drupal::service('messenger');
$sitemapGenerator = new static($database, $configFactory, $fileSystem, $messenger);
$sitemapGenerator
->process();
}
public function process() {
$query = $this->database
->select('file_usage', 'fu');
$query
->fields('fu', array(
'id',
));
$query
->join('node', 'n', 'n.nid = fu.id');
$nids = $query
->distinct()
->execute()
->fetchAll();
$output = '';
$path = '';
$total_urls = 0;
if (count($nids) > 0) {
$output = '<?xml version="1.0" encoding="UTF-8"?>';
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
foreach ($nids as $nid) {
// fetch list of media files for each nid.
$query = \Drupal::database()
->select('file_managed', 'fm');
$query
->fields('fm', array(
'fid',
'filename',
'uri',
));
$query
->join('file_usage', 'fu', 'fu.fid = fm.fid');
$query
->condition('fu.id', $nid->id);
$query
->condition('fm.filemime', 'image/%', 'LIKE');
$files = $query
->execute()
->fetchAll();
if (count($files) > 0) {
$output .= '<url><loc>' . Url::fromRoute('entity.node.canonical', array(
'node' => $nid->id,
), array(
'absolute' => TRUE,
))
->toString() . '</loc>';
foreach ($files as $file) {
$output .= '<image:image><image:loc>' . file_create_url($file->uri) . '</image:loc><image:title>' . $file->filename . '</image:title></image:image>';
}
$output .= '</url>';
$total_urls++;
}
}
$output .= '</urlset>';
// File build path.
$path = file_create_url($this->fileSystem
->realpath($this->configFactory
->get('system.file')
->get('default_scheme') . "://media_sitemap"));
if (!is_dir($path)) {
\Drupal::service('file_system')
->mkdir($path);
}
}
$time = time();
$filename = 'image_sitemap.xml';
if ($file = $this->fileSystem
->saveData($output, $path . '/' . $filename, FileSystemInterface::EXISTS_REPLACE)) {
$this->configFactory
->getEditable('media_sitemap.settings')
->set('image_sitemap_created', $time)
->set('image_sitemap_number_of_urls', $total_urls)
->save();
}
}
public function sitemapGenerateFinished($success, $results, $operations) {
if ($success) {
$this->messenger
->addMessage(t('Image Sitemap Generated Successfully.'));
}
else {
$error_operation = reset($operations);
$this->messenger
->addMessage(t('An error occurred while processing @operation with arguments : @args', array(
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0], TRUE),
)));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaSitemapGenerator:: |
protected | property | ||
MediaSitemapGenerator:: |
protected | property | ||
MediaSitemapGenerator:: |
protected | property | ||
MediaSitemapGenerator:: |
protected | property | ||
MediaSitemapGenerator:: |
public static | function | ||
MediaSitemapGenerator:: |
public | function | ||
MediaSitemapGenerator:: |
public | function | ||
MediaSitemapGenerator:: |
public | function | MediaSitemapGenerator constructor. |