You are here

class AdminHelper in Search and Replace Scanner 8

Shared logic for use in the mdoule.

Hierarchy

Expanded class hierarchy of AdminHelper

1 file declares its use of AdminHelper
Node.php in src/Plugin/Scanner/Node.php

File

src/AdminHelper.php, line 10

Namespace

Drupal\scanner
View source
class AdminHelper {

  /**
   * Show a message on the screen.
   */
  public static function addMessage($message) {
    \Drupal::messenger()
      ->addMessage($message);
  }

  /**
   * Show an error message on the screen.
   */
  public static function addError($message) {
    \Drupal::messenger()
      ->addError($message);
  }

  /**
   * Log or show a notice message on the screen.
   */
  public static function addToLog($message, $DEBUG = FALSE) {
    if ($DEBUG) {
      \Drupal::logger('scanner')
        ->notice($message);
    }
  }

  /**
   * Get all enabled languages, excluding current language.
   */
  public static function getOtherEnabledLanguages() {

    // Get the list of all languages.
    $language = \Drupal::languageManager()
      ->getCurrentLanguage();
    $languages = \Drupal::languageManager()
      ->getLanguages();
    $other_languages = [];

    // Add each enabled language, aside from the current language to an 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;
  }

  /**
   * Get current language.
   */
  public static function getDefaultLangcode() {
    $language = \Drupal::languageManager()
      ->getDefaultLanguage();
    return $language
      ->getId();
  }

  /**
   * Get all enabled languages, including the current language.
   */
  public static function getAllEnabledLanguages() {

    // Get the list of all languages.
    $languages = \Drupal::languageManager()
      ->getLanguages();
    $other_languages = [];

    // Add each enabled language, aside from the current language to an array.
    foreach ($languages as $field_language_code => $field_language) {
      $other_languages[$field_language_code] = $field_language
        ->getName();
    }
    return $other_languages;
  }

  /**
   * Get the latest revision.
   */
  public static function _latest_revision($nid, &$vid, $langcode) {

    // Change record below might be helpful for future improvements.
    // @see https://www.drupal.org/node/2942013
    $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) {

        // There is no pending revision, the current revision is the latest.
        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;
  }

  /**
   * Prepares a new revision of a given entity, if applicable.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   An entity.
   * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $message
   *   A revision log message to set.
   * @param int $current_uid
   *   The user ID of the current logged-in user.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The moderation state for the given entity.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdminHelper::addError public static function Show an error message on the screen.
AdminHelper::addMessage public static function Show a message on the screen.
AdminHelper::addToLog public static function Log or show a notice message on the screen.
AdminHelper::getAllEnabledLanguages public static function Get all enabled languages, including the current language.
AdminHelper::getDefaultLangcode public static function Get current language.
AdminHelper::getOtherEnabledLanguages public static function Get all enabled languages, excluding current language.
AdminHelper::prepareNewRevision public static function Prepares a new revision of a given entity, if applicable.
AdminHelper::_latest_revision public static function Get the latest revision.