View source
<?php
namespace Drupal\moderated_content_bulk_publish;
class AdminHelper {
public static function addMessage($message) {
\Drupal::messenger()
->addMessage($message);
}
public static function addToLog($message, $DEBUG = FALSE) {
if ($DEBUG) {
\Drupal::logger('moderated_content_bulk_publish')
->notice($message);
}
}
public static function getOtherEnabledLanguages() {
$language = \Drupal::languageManager()
->getCurrentLanguage();
$languages = \Drupal::languageManager()
->getLanguages();
$other_languages = array();
foreach ($languages as $field_language_code => $field_language) {
if ($field_language_code != $language
->getId()) {
$other_languages[$field_language_code] = $field_language
->getName();
}
}
return $other_languages;
}
public static function getDefaultLangcode() {
$language = \Drupal::languageManager()
->getDefaultLanguage();
return $language
->getId();
}
public static function getAllEnabledLanguages() {
$other_languages = self::getOtherEnabledLanguages();
$language = \Drupal::languageManager()
->getCurrentLanguage();
$current_language = [];
$other_languages = array_reverse($other_languages);
$all_languages = $other_languages;
$all_languages[$language
->getId()] = $language
->getName();
$all_languages = array_reverse($all_languages);
return $all_languages;
}
public static function bulkPublishShutdown($entity, $langcode, $moderation_state) {
if (!empty($moderation_state)) {
$vid = 0;
$latest_revision = self::bulkLatestRevision($entity
->id(), $vid, $langcode, $entity
->getEntityTypeId());
$latest_state = $moderation_state;
$latest_is_valid = TRUE;
if ($latest_revision == FALSE) {
$latest_is_valid = FALSE;
}
else {
$latest_state = $latest_revision
->get('moderation_state')
->getString();
}
if ($latest_is_valid) {
$latest_revision
->setSyncing(TRUE);
$latest_revision
->setRevisionTranslationAffected(TRUE);
$latest_revision
->set('moderation_state', $moderation_state);
$latest_revision
->save();
\Drupal::service('pathauto.generator')
->updateEntityAlias($latest_revision, 'insert', array(
'language' => $langcode,
));
\Drupal::service('pathauto.generator')
->updateEntityAlias($latest_revision, 'update', array(
'language' => $langcode,
));
}
else {
\Drupal::service('pathauto.generator')
->updateEntityAlias($entity, 'insert', array(
'language' => $langcode,
));
\Drupal::service('pathauto.generator')
->updateEntityAlias($entity, 'update', array(
'language' => $langcode,
));
}
}
else {
\Drupal::service('pathauto.generator')
->updateEntityAlias($entity, 'insert', array(
'language' => $langcode,
));
\Drupal::service('pathauto.generator')
->updateEntityAlias($entity, 'update', array(
'language' => $langcode,
));
}
}
public static function bulkLatestRevision($id, &$vid, $lang, $entity_type = 'node') {
$query = \Drupal::entityTypeManager()
->getStorage($entity_type)
->getQuery();
$query
->latestRevision();
if ($entity_type == 'node') {
$query
->condition('nid', $id, '=');
}
if ($entity_type == 'taxonomy_term') {
$query
->condition('tid', $id, '=');
}
if ($entity_type == 'paragraph') {
$query
->condition('id', $id, '=');
}
if ($entity_type == 'user') {
$query
->condition('uid', $id, '=');
}
$latestRevisionResult = $query
->execute();
if (count($latestRevisionResult)) {
$node_revision_id = key($latestRevisionResult);
$vid = $node_revision_id;
$latestRevision = \Drupal::entityTypeManager()
->getStorage($entity_type)
->loadRevision($node_revision_id);
if ($latestRevision
->hasTranslation($lang) && $latestRevision
->language()
->getId() != $lang) {
$latestRevision = $latestRevision
->getTranslation($lang);
}
return $latestRevision;
}
return FALSE;
}
}