View source
<?php
namespace Drupal\scanner;
use Drupal\Core\Entity\EntityInterface;
class AdminHelper {
public static function addMessage($message) {
\Drupal::messenger()
->addMessage($message);
}
public static function addError($message) {
\Drupal::messenger()
->addError($message);
}
public static function addToLog($message, $DEBUG = FALSE) {
if ($DEBUG) {
\Drupal::logger('scanner')
->notice($message);
}
}
public static function getOtherEnabledLanguages() {
$language = \Drupal::languageManager()
->getCurrentLanguage();
$languages = \Drupal::languageManager()
->getLanguages();
$other_languages = [];
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() {
$languages = \Drupal::languageManager()
->getLanguages();
$other_languages = [];
foreach ($languages as $field_language_code => $field_language) {
$other_languages[$field_language_code] = $field_language
->getName();
}
return $other_languages;
}
public static function _latest_revision($nid, &$vid, $langcode) {
$lang = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
if (!isset($langcode)) {
$langcode = $lang;
}
if ($lang != $langcode) {
$lang = $langcode;
}
$latestRevisionResult = \Drupal::entityTypeManager()
->getStorage('node')
->getQuery()
->latestRevision()
->condition('nid', $nid, '=')
->execute();
if (count($latestRevisionResult)) {
$node_revision_id = key($latestRevisionResult);
if ($node_revision_id == $vid) {
return FALSE;
}
$vid = $node_revision_id;
$latestRevision = \Drupal::entityTypeManager()
->getStorage('node')
->loadRevision($node_revision_id);
if ($latestRevision
->language()
->getId() != $lang && $latestRevision
->hasTranslation($lang)) {
$latestRevision = $latestRevision
->getTranslation($lang);
}
return $latestRevision;
}
return FALSE;
}
public static function prepareNewRevision(EntityInterface $entity, $message, $current_uid) {
$storage = \Drupal::entityTypeManager()
->getStorage($entity
->getEntityTypeId());
if ($storage instanceof ContentEntityStorageInterface) {
$revision = $storage
->createRevision($entity);
if ($revision instanceof RevisionLogInterface) {
$revision
->setRevisionLogMessage($message);
$revision
->setRevisionCreationTime(\Drupal::time()
->getRequestTime());
$revision
->setRevisionUserId($current_uid);
}
return $revision;
}
return $entity;
}
}