Commit 6ed7846c authored by amateescu's avatar amateescu Committed by amateescu

Issue #2781603 by amateescu: Add support for entity translations

parent ab4fb5fd
......@@ -121,7 +121,7 @@ class XmlSitemapLinkStorage implements XmlSitemapLinkStorageInterface {
}
$queryStatus = \Drupal::database()->merge('xmlsitemap')
->key(array('type' => $link['type'], 'id' => $link['id']))
->key(array('type' => $link['type'], 'id' => $link['id'], 'language' => $link['language']))
->fields(array(
'loc' => $link['loc'],
'subtype' => $link['subtype'],
......@@ -133,7 +133,6 @@ class XmlSitemapLinkStorage implements XmlSitemapLinkStorageInterface {
'priority_override' => $link['priority_override'],
'changefreq' => $link['changefreq'],
'changecount' => $link['changecount'],
'language' => $link['language'],
))
->execute();
......@@ -212,8 +211,11 @@ class XmlSitemapLinkStorage implements XmlSitemapLinkStorageInterface {
/**
* {@inheritdoc}
*/
public function delete($entity_type, $entity_id) {
public function delete($entity_type, $entity_id, $langcode = NULL) {
$conditions = array('type' => $entity_type, 'id' => $entity_id);
if ($langcode) {
$conditions['language'] = $langcode;
}
return $this->deleteMultiple($conditions);
}
......
......@@ -70,11 +70,14 @@ interface XmlSitemapLinkStorageInterface {
* A string with the entity type.
* @param $entity_id
* Entity ID to be deleted.
* @param string $langcode
* (optional) The language code for the link that should be deleted.
* If omitted, links for that entity will be removed in all languages.
*
* @return
* The number of links that were deleted.
*/
public function delete($entity_type, $entity_id);
public function delete($entity_type, $entity_id, $langcode = NULL);
/**
* Delete multiple sitemap links from the database.
......
......@@ -6,6 +6,8 @@
*
* @ingroup xmlsitemap
*/
use Drupal\Core\Database\Database;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Url;
......@@ -261,12 +263,11 @@ function xmlsitemap_schema() {
'default' => 0,
),
),
'primary key' => array('id', 'type'),
'primary key' => array('id', 'type', 'language'),
'indexes' => array(
'loc' => array('loc'),
'access_status_loc' => array('access', 'status', 'loc'),
'type_subtype' => array('type', 'subtype'),
'language' => array('language'),
),
);
......@@ -342,8 +343,10 @@ function xmlsitemap_uninstall() {
}
/**
* Implements hook_update_last_removed().
* Change the primary key of the 'xmlsitemap' table to include the language.
*/
function xmlsitemap_update_last_removed() {
return 7203;
function xmlsitemap_update_8001() {
Database::getConnection()->schema()->dropPrimaryKey('xmlsitemap');
Database::getConnection()->schema()->addPrimaryKey('xmlsitemap', array('id', 'type', 'language'));
Database::getConnection()->schema()->dropIndex('xmlsitemap', 'language');
}
......@@ -13,8 +13,8 @@ use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Routing\RouteMatchInterface;
......@@ -1535,8 +1535,9 @@ function xmlsitemap_xmlsitemap_index_links($limit) {
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$bundles = \Drupal::service('entity_type.bundle.info')->getAllBundleInfo();
foreach ($entity_types as $entity_type_id => $entity_type) {
// Don't try to loop over entity types with no bundle defined.
if (!isset($bundles[$entity_type_id])) {
// Don't try to loop over non-content entity types or with no bundle
// defined.
if (!$entity_type instanceof ContentEntityTypeInterface || !isset($bundles[$entity_type_id])) {
continue;
}
......@@ -1568,41 +1569,24 @@ function xmlsitemap_xmlsitemap_index_links($limit) {
/**
* Process sitemap links.
*
* @param array $entities
* An array of \Drupal\Core\Entity\EntityInterface objects.
* @param array
* Entity ids to be processed.
*/
function xmlsitemap_xmlsitemap_process_entity_links($entity_type, array $ids) {
$entities = entity_load_multiple($entity_type, $ids);
$anonymous_user = new AnonymousUserSession();
foreach ($entities as $entity) {
$link_storage = \Drupal::service('xmlsitemap.link_storage');
$link = $link_storage->create($entity);
$link_storage->save($link);
}
}
/**
* Implements hook_entity_presave().
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that will be presaved.
* @param string $entity_type_id
* The entity type to process.
* @param array $entity_ids
* Entity IDs to be processed.
*/
function xmlsitemap_entity_presave(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$anonymous_user = new AnonymousUserSession();
function xmlsitemap_xmlsitemap_process_entity_links($entity_type_id, array $entity_ids) {
$link_storage = \Drupal::service('xmlsitemap.link_storage');
if (!xmlsitemap_link_bundle_check_enabled($entity_type, $bundle)) {
return;
}
/** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
$entities = \Drupal::entityTypeManager()->getStorage($entity_type_id)->loadMultiple($entity_ids);
foreach ($entities as $entity) {
// Generate an entry for each translation.
foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$translation = $entity->getTranslation($langcode);
$id = $entity->id();
if (!empty($id)) {
$link_storage = \Drupal::service('xmlsitemap.link_storage');
$link = $link_storage->create($entity);
$link_storage->save($link);
$link = $link_storage->create($translation);
$link_storage->save($link);
}
}
}
......@@ -1613,17 +1597,19 @@ function xmlsitemap_entity_presave(EntityInterface $entity) {
* The entity that will be inserted.
*/
function xmlsitemap_entity_insert(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$anonymous_user = new AnonymousUserSession();
if (!xmlsitemap_link_bundle_check_enabled($entity_type, $bundle)) {
if (!xmlsitemap_link_bundle_check_enabled($entity->getEntityTypeId(), $entity->bundle())) {
return;
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$link_storage = \Drupal::service('xmlsitemap.link_storage');
$link = $link_storage->create($entity);
$link_storage->save($link);
// 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);
}
}
/**
......@@ -1633,17 +1619,19 @@ function xmlsitemap_entity_insert(EntityInterface $entity) {
* The entity that will be updated.
*/
function xmlsitemap_entity_update(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$anonymous_user = new AnonymousUserSession();
if (!xmlsitemap_link_bundle_check_enabled($entity_type, $bundle)) {
if (!xmlsitemap_link_bundle_check_enabled($entity->getEntityTypeId(), $entity->bundle())) {
return;
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$link_storage = \Drupal::service('xmlsitemap.link_storage');
$link = $link_storage->create($entity);
$link_storage->save($link);
// 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);
}
}
/**
......@@ -1653,10 +1641,15 @@ function xmlsitemap_entity_update(EntityInterface $entity) {
* The entity that will be deleted.
*/
function xmlsitemap_entity_delete(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$langcode = $entity->language() ? $entity->language()->getId() : NULL;
\Drupal::service('xmlsitemap.link_storage')->delete($entity->getEntityTypeId(), $entity->id(), $langcode);
}
\Drupal::service('xmlsitemap.link_storage')->delete($entity->getEntityTypeId(), $entity->id());
/**
* Implements hook_entity_translation_delete().
*/
function xmlsitemap_entity_translation_delete(EntityInterface $translation) {
\Drupal::service('xmlsitemap.link_storage')->delete($translation->getEntityTypeId(), $translation->id(), $translation->language()->getId());
}
/**
......
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