You are here

class PublishContentPublishEntity in Publish Content 8

Toggles node status.

Hierarchy

Expanded class hierarchy of PublishContentPublishEntity

File

src/Controller/PublishContentPublishEntity.php, line 17

Namespace

Drupal\publishcontent\Controller
View source
class PublishContentPublishEntity implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The ServerBag object from the current request.
   *
   * @var \Symfony\Component\HttpFoundation\ServerBag
   */
  protected $server;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The current user service.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The module configuration for reading.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The logging channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = new static();
    $instance->server = $container
      ->get('request_stack')
      ->getCurrentRequest()->server;
    $instance->languageManager = $container
      ->get('language_manager');
    $instance->messenger = $container
      ->get('messenger');
    $instance->config = $container
      ->get('config.factory')
      ->get('publishcontent.settings');
    $instance->currentUser = $container
      ->get('current_user');
    $instance->logger = $container
      ->get('logger.factory')
      ->get('publishcontent');
    return $instance;
  }

  /**
   * Toggle node status.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node being toggled.
   * @param string $langcode
   *   The language code of the node.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Redirect to the previous page.
   */
  public function toggleEntityStatus(NodeInterface $node, $langcode = '') {
    try {
      if ($referrer = $this->server
        ->get('HTTP_REFERER')) {
        $redirectUrl = Url::fromUri($referrer, [
          'absolute' => TRUE,
        ])
          ->getUri();
      }
      else {
        $redirectUrl = $node
          ->toUrl()
          ->toString();
      }
    } catch (\Exception $e) {
      $redirectUrl = Url::fromRoute('<front>')
        ->setAbsolute()
        ->toString();
    }
    if ($node
      ->isTranslatable()) {
      if ($langcode == '') {
        $langcode = $this->languageManager
          ->getCurrentLanguage()
          ->getId();
      }
      if (!$node
        ->hasTranslation($langcode)) {
        $this->messenger
          ->addError($this
          ->t("You can't @publish/@unpublish a non-existing translation.", [
          '@publish' => $this->config
            ->get('publish_text_value'),
          '@unpublish' => $this->config
            ->get('unpublish_text_value'),
        ]));
        return new RedirectResponse($redirectUrl);
      }
      $node = $node
        ->getTranslation($langcode);
    }
    $node
      ->isPublished() ? $node
      ->setUnpublished() : $node
      ->setPublished();
    $isPublished = $node
      ->isPublished();
    $status = $isPublished ? $this->config
      ->get('publish_text_value') : $this->config
      ->get('unpublish_text_value');
    if (!empty($this->config)) {
      if ($this->config
        ->get('create_log_entry')) {
        $this->logger
          ->notice($this
          ->t('@type: @action @title', [
          '@type' => $node
            ->bundle(),
          '@action' => $isPublished ? 'unpublished' : 'published',
          '@title' => $node
            ->getTitle(),
        ]));
      }
      if ($this->config
        ->get('create_revision')) {
        $node
          ->setNewRevision(TRUE);
        $node->revision_log = $this
          ->t('Changed to @status by @user', [
          '@status' => $status,
          '@user' => $this->currentUser
            ->getDisplayName(),
        ]);
        $node
          ->setRevisionCreationTime(REQUEST_TIME);
        $node
          ->setRevisionUserId($this->currentUser
          ->id());
      }
    }
    try {
      $node
        ->save();
      $this->messenger
        ->addMessage($this
        ->t('@title has been set to @status', [
        '@title' => $node
          ->getTitle(),
        '@status' => $status,
      ]));
    } catch (EntityStorageException $e) {
    }
    return new RedirectResponse($redirectUrl);
  }

  /**
   * A custom route access callback for the Publish/Unpublish local task UI.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function hasUILocalTask() {
    return AccessResult::allowedIf(!empty($this->config) && !empty($this->config
      ->get('ui_localtask')));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PublishContentPublishEntity::$config protected property The module configuration for reading.
PublishContentPublishEntity::$currentUser protected property The current user service.
PublishContentPublishEntity::$languageManager protected property The language manager.
PublishContentPublishEntity::$logger protected property The logging channel.
PublishContentPublishEntity::$messenger protected property The messenger.
PublishContentPublishEntity::$server protected property The ServerBag object from the current request.
PublishContentPublishEntity::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
PublishContentPublishEntity::hasUILocalTask public function A custom route access callback for the Publish/Unpublish local task UI.
PublishContentPublishEntity::toggleEntityStatus public function Toggle node status.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.