SimpleSitemapSyncExtend.php in CMS Content Sync 2.0.x
File
modules/cms_content_sync_simple_sitemap/src/EventSubscriber/SimpleSitemapSyncExtend.php
View source
<?php
namespace Drupal\cms_content_sync_simple_sitemap\EventSubscriber;
use Drupal\cms_content_sync\Event\BeforeEntityPush;
use Drupal\cms_content_sync\Event\BeforeEntityTypeExport;
use Drupal\cms_content_sync\Event\BeforeEntityPull;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SimpleSitemapSyncExtend implements EventSubscriberInterface {
const PROPERTY_NAME = 'simple_sitemap';
public static function getSubscribedEvents() {
$events[BeforeEntityPush::EVENT_NAME][] = [
'extendPush',
];
$events[BeforeEntityPull::EVENT_NAME][] = [
'extendPull',
];
$events[BeforeEntityTypeExport::EVENT_NAME][] = [
'extendEntityType',
];
return $events;
}
protected function sitemapSupportsEntityType($entity_type_name, $bundle_name) {
$entity_type_manager = \Drupal::service('entity_type.manager');
$entity_type = $entity_type_manager
->getDefinition($entity_type_name, FALSE);
if (!$entity_type instanceof ContentEntityTypeInterface || !method_exists($entity_type, 'getBundleEntityType') || !$entity_type
->hasLinkTemplate('canonical')) {
return FALSE;
}
$config_factory = \Drupal::service('config.factory');
$setting = $config_factory
->get('simple_sitemap.settings')
->get('enabled_entity_types');
if (empty($setting) || !in_array($entity_type_name, $setting)) {
return FALSE;
}
$bundle_settings = $config_factory
->get("simple_sitemap.bundle_settings.{$entity_type_name}.{$bundle_name}")
->get();
if (empty($bundle_settings)) {
$bundle_settings = $config_factory
->get("simple_sitemap.bundle_settings.default.{$entity_type_name}.{$bundle_name}")
->get();
}
if (empty($bundle_settings) || empty($bundle_settings['index'])) {
return FALSE;
}
return TRUE;
}
public function extendEntityType(BeforeEntityTypeExport $event) {
if (!$this
->sitemapSupportsEntityType($event
->getEntityTypeName(), $event
->getBundleName())) {
return;
}
$event
->getDefinition()
->addObjectProperty(self::PROPERTY_NAME, 'Simple sitemap', FALSE);
}
public function extendPush(BeforeEntityPush $event) {
$intent = $event->intent;
$entity = $event->entity;
if (!$this
->sitemapSupportsEntityType($entity
->getEntityTypeId(), $entity
->bundle())) {
return;
}
$values = _cms_content_sync_submit_cache($entity
->getEntityTypeId(), $entity
->uuid());
if (empty($values)) {
$generator = \Drupal::service('simple_sitemap.generator');
$values = $generator
->getEntityInstanceSettings($entity
->getEntityTypeId(), $entity
->id());
$intent
->setProperty(self::PROPERTY_NAME, $values);
return;
}
else {
$values = isset($values['simple_sitemap']) ? $values['simple_sitemap'] : $values;
}
if (empty($values)) {
$intent
->setProperty(self::PROPERTY_NAME, NULL);
return;
}
$values = [
'index' => empty($values['simple_sitemap_index_content']) ? empty($values['index_default_node_settings']) ? NULL : $values['index_default_node_settings'] : $values['simple_sitemap_index_content'],
'priority' => empty($values['simple_sitemap_priority']) ? empty($values['priority_default_node_settings']) ? NULL : $values['priority_default_node_settings'] : $values['simple_sitemap_priority'],
'changefreq' => empty($values['simple_sitemap_changefreq']) ? empty($values['changefreq_default_node_settings']) ? NULL : $values['changefreq_default_node_settings'] : $values['simple_sitemap_changefreq'],
'include_images' => empty($values['simple_sitemap_include_images']) ? empty($values['include_images_default_node_settings']) ? NULL : $values['include_images_default_node_settings'] : $values['simple_sitemap_include_images'],
];
$intent
->setProperty(self::PROPERTY_NAME, $values);
}
public function extendPull(BeforeEntityPull $event) {
$intent = $event->intent;
$entity = $intent
->getEntity();
if (!$this
->sitemapSupportsEntityType($entity
->getEntityTypeId(), $entity
->bundle())) {
return;
}
$values = $intent
->getProperty(self::PROPERTY_NAME);
if (empty($values)) {
return;
}
$generator = \Drupal::service('simple_sitemap.generator');
$generator
->setEntityInstanceSettings($entity
->getEntityTypeId(), $entity
->id(), $values);
}
}