Unverified Commit 55c8929a authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #2387341 by Dave Reid: Allow for additional context to be passed through...

Issue #2387341 by Dave Reid: Allow for additional context to be passed through xmlsitemap_link_save().
parent 126cf96e
......@@ -98,7 +98,7 @@ class XmlSitemapLinkStorage implements XmlSitemapLinkStorageInterface {
/**
* {@inheritdoc}
*/
public function save(array $link) {
public function save(array $link, array $context = []) {
$link += [
'access' => 1,
'status' => 1,
......@@ -112,7 +112,7 @@ class XmlSitemapLinkStorage implements XmlSitemapLinkStorageInterface {
];
// Allow other modules to alter the link before saving.
$this->moduleHandler->alter('xmlsitemap_link', $link);
$this->moduleHandler->alter('xmlsitemap_link', $link, $context);
// Temporary validation checks.
// @todo Remove in final?
......@@ -157,11 +157,11 @@ class XmlSitemapLinkStorage implements XmlSitemapLinkStorageInterface {
switch ($queryStatus) {
case Merge::STATUS_INSERT:
$this->moduleHandler->invokeAll('xmlsitemap_link_insert', [$link]);
$this->moduleHandler->invokeAll('xmlsitemap_link_insert', [$link, $context]);
break;
case Merge::STATUS_UPDATE:
$this->moduleHandler->invokeAll('xmlsitemap_link_update', [$link]);
$this->moduleHandler->invokeAll('xmlsitemap_link_update', [$link, $context]);
break;
}
......
......@@ -62,11 +62,10 @@ function hook_xmlsitemap_link_info() {
*
* @param array $link
* An array with the data of the sitemap link.
*
* @codingStandardsIgnoreStart
* @param array $context
* An optional context array containing data related to the link.
*/
function hook_xmlsitemap_link_alter(&$link) {
// @codingStandardsIgnoreEnd
function hook_xmlsitemap_link_alter(array &$link, array $context) {
if ($link['type'] == 'mymodule') {
$link['priority'] += 0.5;
}
......@@ -78,10 +77,12 @@ function hook_xmlsitemap_link_alter(&$link) {
* @param array $link
* Associative array defining an XML sitemap link as passed into
* \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface::save().
* @param array $context
* An optional context array containing data related to the link.
*
* @see hook_xmlsitemap_link_update()
*/
function hook_xmlsitemap_link_insert(array $link) {
function hook_xmlsitemap_link_insert(array $link, array $context) {
\Drupal::database()->insert('mytable')
->fields([
'link_type' => $link['type'],
......@@ -97,10 +98,12 @@ function hook_xmlsitemap_link_insert(array $link) {
* @param array $link
* Associative array defining an XML sitemap link as passed into
* \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface::save().
* @param array $context
* An optional context array containing data related to the link.
*
* @see hook_xmlsitemap_link_insert()
*/
function hook_xmlsitemap_link_update(array $link) {
function hook_xmlsitemap_link_update(array $link, array $context) {
\Drupal::database()->update('mytable')
->fields([
'link_type' => $link['type'],
......@@ -120,7 +123,7 @@ function hook_xmlsitemap_link_update(array $link) {
*/
function hook_xmlsitemap_rebuild_clear(array $entity_type_ids, $save_custom) {
\Drupal::database()->delete('mytable')
->condition('link_type', $types, 'IN')
->condition('link_type', $entity_type_ids, 'IN')
->execute();
}
......
......@@ -13,22 +13,23 @@
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Link;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Entity\Query\QueryException;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\xmlsitemap\Entity\XmlSitemap;
use Drupal\xmlsitemap\XmlSitemapInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* The maximum number of links in one sitemap chunk file.
......@@ -1625,38 +1626,61 @@ function xmlsitemap_xmlsitemap_index_links($limit) {
* Entity IDs to be processed.
*/
function xmlsitemap_xmlsitemap_process_entity_links($entity_type_id, array $entity_ids) {
$link_storage = \Drupal::service('xmlsitemap.link_storage');
/** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
$entities = \Drupal::entityTypeManager()->getStorage($entity_type_id)->loadMultiple($entity_ids);
foreach ($entities as $entity) {
xmlsitemap_xmlsitemap_process_entity_link($entity);
}
\Drupal::entityTypeManager()->getStorage($entity_type_id)->resetCache();
}
/**
* Process sitemap link for an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
*/
function xmlsitemap_xmlsitemap_process_entity_link(EntityInterface $entity) {
/** @var \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage */
$link_storage = \Drupal::service('xmlsitemap.link_storage');
if ($entity instanceof ContentEntityInterface) {
// Generate an entry for each translation.
foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$translation_languages = $entity->getTranslationLanguages();
foreach ($translation_languages as $langcode => $language) {
$translation = $entity->getTranslation($langcode);
$link = $link_storage->create($translation);
$link_storage->save($link);
$context = [$link['type'] => $entity, 'entity' => $entity];
$link_storage->save($link, $context);
}
// Check if there are any removed language translations.
if (isset($entity->original)) {
$original_translation_languages = $entity->original->getTranslationLanguages();
$removed_translation_languages = array_diff(array_keys($original_translation_languages), array_keys($translation_languages));
if (!empty($removed_translation_languages)) {
$link_storage->deleteMultiple([
'type' => $entity->getEntityTypeId(),
'id' => $entity->id(),
'language' => $removed_translation_languages,
]);
}
}
}
\Drupal::entityTypeManager()->getStorage($entity_type_id)->resetCache();
else {
$link = $link_storage->create($entity);
$context = [$link['type'] => $entity, 'entity' => $entity];
$link_storage->save($link, $context);
}
}
/**
* Implements hook_entity_insert().
*/
function xmlsitemap_entity_insert(EntityInterface $entity) {
if (!xmlsitemap_link_bundle_check_enabled($entity->getEntityTypeId(), $entity->bundle())) {
return;
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$link_storage = \Drupal::service('xmlsitemap.link_storage');
// Generate an entry for each translation.
foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$translation = $entity->getTranslation($langcode);
$link = $link_storage->create($translation);
$link_storage->save($link);
if (xmlsitemap_link_bundle_check_enabled($entity->getEntityTypeId(), $entity->bundle())) {
xmlsitemap_xmlsitemap_process_entity_link($entity);
}
}
......@@ -1664,36 +1688,32 @@ function xmlsitemap_entity_insert(EntityInterface $entity) {
* Implements hook_entity_update().
*/
function xmlsitemap_entity_update(EntityInterface $entity) {
if (!xmlsitemap_link_bundle_check_enabled($entity->getEntityTypeId(), $entity->bundle())) {
return;
}
if (xmlsitemap_link_bundle_check_enabled($entity->getEntityTypeId(), $entity->bundle())) {
xmlsitemap_xmlsitemap_process_entity_link($entity);
// Check if we have differences between the translations from the link
// storage and the current translations of the entity, and if so, remove
// them.
if ($entity instanceof ContentEntityInterface) {
$translation_languages = $entity->getTranslationLanguages();
$link_storage = \Drupal::service('xmlsitemap.link_storage');
$link_storage_languages = [];
$links = $link_storage->loadMultiple([
'type' => $entity->getEntityTypeId(),
'id' => $entity->id(),
]);
foreach ($links as $link) {
$link_storage_languages[] = $link['language'];
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$link_storage = \Drupal::service('xmlsitemap.link_storage');
// Generate an entry for each translation.
$translation_languages = $entity->getTranslationLanguages();
foreach ($translation_languages as $langcode => $language) {
$translation = $entity->getTranslation($langcode);
$link = $link_storage->create($translation);
$link_storage->save($link);
}
// Check if we have differences between the translations from the link
// storage and the current translations of the entity.
$link_storage_languages = [];
$links = $link_storage->loadMultiple(['type' => $entity->getEntityTypeId(), 'id' => $entity->id()]);
foreach ($links as $link) {
$link_storage_languages[] = $link['language'];
}
// If so, remove the translations that no longer exist.
if ($language_differences = array_diff($link_storage_languages, array_keys($translation_languages))) {
$link_storage->deleteMultiple([
'type' => $entity->getEntityTypeId(),
'id' => $entity->id(),
'language' => $language_differences,
]);
if ($language_differences = array_diff($link_storage_languages, array_keys($translation_languages))) {
$link_storage->deleteMultiple([
'type' => $entity->getEntityTypeId(),
'id' => $entity->id(),
'language' => $language_differences,
]);
}
}
}
}
......
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