Commit 8b06e1eb authored by andrei.dincu's avatar andrei.dincu

Issue #2249723 by andrei.dincu: Create xmlsitemap_entity_presave(),...

Issue #2249723 by andrei.dincu: Create xmlsitemap_entity_presave(), xmlsitemap_entity_insert(), xmlsitemap_entity_update(), xmlsitemap_entity_delete() and other additional functions in xmlsitemap.module
parent 1419359a
......@@ -19,6 +19,8 @@ use Drupal\Component\Utility\String;
use Drupal\Core\Render\Element;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Entity;
/**
* The maximum number of links in one sitemap chunk file.
......@@ -262,10 +264,7 @@ function xmlsitemap_cron() {
if ($lifetime < \Drupal::config('xmlsitemap.settings')->get('minimum_lifetine')) {
return;
}
if (\Drupal::state()->get('xmlsitemap_entity_user')) {
xmlsitemap_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
}
xmlsitemap_xmlsitemap_index_links(100);
// Regenerate the sitemap XML files.
module_load_include('generate.inc', 'xmlsitemap');
xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
......@@ -1705,113 +1704,150 @@ function xmlsitemap_form_alter(array &$form, array &$form_state) {
module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
xmlsitemap_add_form_link_options($form, $entity_type, $bundle, $entity->id());
$form['xmlsitemap']['#weight'] = 100;
}
/**
* Implements hook_xmlsitemap_index_links().
*/
function xmlsitemap_xmlsitemap_index_links($limit) {
$entity_types = \Drupal::entityManager()->getDefinitions();
$bundles = \Drupal::entityManager()->getAllBundleInfo();
foreach ($entity_types as $entity_type_id => $entity_type) {
$entity_bundles = array();
if (!\Drupal::state()->get('xmlsitemap_entity_' . $entity_type_id, 0)) {
continue;
}
foreach ($bundles[$entity_type_id] as $bundle => $bundle_info) {
if (!\Drupal::state()->get('xmlsitemap_entity_' . $entity_type_id . '_bundle_' . $bundle, 0)) {
continue;
}
$entity_bundles[] = $bundle;
}
$query = \Drupal::entityManager()->getStorage($entity_type_id)->getQuery();
$query->condition('bundle', $entity_bundles, 'in');
$entities = $query->execute();
xmlsitemap_xmlsitemap_process_entity_links($entities);
}
}
/**
* Process user sitemap links.
*
* @param $uids
* An array of user IDs.
* @param $entities
* An array of .
*/
/*function xmlsitemap_user_xmlsitemap_process_user_links(array $uids) {
if (!\Drupal::state()->get('xmlsitemap_entity_user')) {
return;
}
$accounts = User::loadMultiple($uids);
foreach ($accounts as $account) {
$link = xmlsitemap_user_create_link($account);
function xmlsitemap_xmlsitemap_process_entity_links(array $entities) {
foreach ($entities as $entity) {
$link = xmlsitemap_user_create_link($entity);
xmlsitemap_link_save($link);
}
}*/
}
/**
* Implements hook_user_presave().
* Implements hook_entity_presave().
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that will be presaved.
*/
/*function xmlsitemap_user_presave(UserInterface $account) {
if (!\Drupal::state()->get('xmlsitemap_entity_user')) {
function xmlsitemap_entity_presave(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
if (!\Drupal::state()->get('xmlsitemap_entity_' . $entity_type . '_bundle_' . $bundle, 0)) {
return;
}
$uid = $account->id();
if (!empty($uid)) {
$link = xmlsitemap_user_create_link($account);
if (isset($account->xmlsitemap)) {
$link = $account->xmlsitemap + $link;
//unset($edit['xmlsitemap']);
}
$id = $entity->id();
if (!empty($id)) {
$link = xmlsitemap_entity_create_link($entity);
/* if (isset($edit['xmlsitemap'])) {
$link = $edit['xmlsitemap'] + $link;
unset($edit['xmlsitemap']);
} */
xmlsitemap_link_save($link);
}
}*/
}
/**
* Implements hook_user_insert().
* Implements hook_entity_insert().
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that will be inserted.
*/
/*function xmlsitemap_user_insert(UserInterface $account) {
if (!\Drupal::state()->get('xmlsitemap_entity_user')) {
function xmlsitemap_entity_insert(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
if (!\Drupal::state()->get('xmlsitemap_entity_' . $entity_type . '_bundle_' . $bundle, 0)) {
return;
}
$link = xmlsitemap_user_create_link($account);
$link = xmlsitemap_entity_create_link($entity);
xmlsitemap_link_save($link);
}*/
}
/**
* Implements hook_user_update().
* Implements hook_entity_update().
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that will be updated.
*/
/*function xmlsitemap_user_update(UserInterface $account) {
if (!\Drupal::state()->get('xmlsitemap_entity_user')) {
function xmlsitemap_entity_update(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
if (!\Drupal::state()->get('xmlsitemap_entity_' . $entity_type . '_bundle_' . $bundle, 0)) {
return;
}
$link = xmlsitemap_user_create_link($account);
$link = xmlsitemap_entity_create_link($entity);
xmlsitemap_link_save($link);
}*/
}
/**
* Implements hook_user_delete().
* Implements hook_entity_delete().
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that will be deleted.
*/
/*function xmlsitemap_user_delete(UserInterface $account) {
xmlsitemap_link_delete('user', $account->id());
}*/
function xmlsitemap_entity_delete(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
if (!\Drupal::state()->get('xmlsitemap_entity_' . $entity_type . '_bundle_' . $bundle, 0)) {
return;
}
xmlsitemap_link_delete($entity->getEntityTypeId(), $entity->id());
}
/**
* Implements hook_field_extra_fields().
*/
/*function xmlsitemap_field_extra_fields() {
/* function xmlsitemap_field_extra_fields() {
$extras['user']['user']['form']['xmlsitemap'] = array(
'label' => t('XML sitemap'),
'description' => t('XML sitemap module element'),
'weight' => 30,
'label' => t('XML sitemap'),
'description' => t('XML sitemap module element'),
'weight' => 30,
);
return $extras;
}*/
} */
/**
* Create a sitemap link from a user.
* Create a sitemap link from an entity.
*
* The link will be saved as $account->xmlsitemap.
* The link will be saved as $entity->xmlsitemap.
*
* @param $account
* A user object.
*/
/*function xmlsitemap_user_create_link(UserInterface &$account) {
if (!isset($account->xmlsitemap)) {
$account->xmlsitemap = array();
if ($account->id() && $link = xmlsitemap_link_load('user', $account->id())) {
$account->xmlsitemap = $link;
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity whose sitemap link will be created.
*/
function xmlsitemap_entity_create_link(EntityInterface $entity) {
if (!isset($entity->xmlsitemap)) {
$entity->xmlsitemap = array();
if ($entity->id() && $link = xmlsitemap_link_load($entity->getEntityTypeId(), $entity->id())) {
$entity->xmlsitemap = $link;
}
}
$settings = xmlsitemap_link_bundle_load('user', 'user');
$uri = $account->url();
$account->xmlsitemap += array(
'type' => 'user',
'id' => $account->id(),
'subtype' => 'user',
$settings = xmlsitemap_link_bundle_load($entity->getEntityTypeId(), $entity->label());
$uri = $entity->url();
$entity->xmlsitemap += array(
'type' => $entity->getEntityTypeId(),
'id' => $entity->id(),
'subtype' => $entity->label(),
'status' => $settings['status'],
'status_default' => $settings['status'],
'status_override' => 0,
......@@ -1821,16 +1857,11 @@ function xmlsitemap_xmlsitemap_index_links($limit) {
);
// The following values must always be checked because they are volatile.
$account->xmlsitemap['loc'] = $uri;
if ($account->isActive()) {
$status = 1;
}
else {
$status = 0;
}
$account->xmlsitemap['access'] = $account->url() && $status;
$language = $account->language();
$account->xmlsitemap['language'] = !empty($language) ? $language->getId() : LanguageInterface::LANGCODE_NOT_SPECIFIED;
$entity->xmlsitemap['loc'] = $uri;
$entity->xmlsitemap['access'] = $entity->url() && $status;
$entity->xmlsitemap['status'] = 1;
$language = $entity->language();
$entity->xmlsitemap['language'] = !empty($language) ? $language->getId() : LanguageInterface::LANGCODE_NOT_SPECIFIED;
return $account->xmlsitemap;
}*/
return $entity->xmlsitemap;
}
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