You are here

class UnpublishedNodesRedirectUtils in Unpublished Nodes Redirect 2.x

Same name and namespace in other branches
  1. 8 src/Utils/UnpublishedNodesRedirectUtils.php \Drupal\unpublished_nodes_redirect\Utils\UnpublishedNodesRedirectUtils

Utility class for Unpublished nodes redirect module.

Hierarchy

Expanded class hierarchy of UnpublishedNodesRedirectUtils

3 files declare their use of UnpublishedNodesRedirectUtils
UnpublishedNodesRedirectOn403Subscriber.php in src/EventSubscriber/UnpublishedNodesRedirectOn403Subscriber.php
UnpublishedNodesRedirectSettingsForm.php in src/Form/UnpublishedNodesRedirectSettingsForm.php
UnpublishedNodesRedirectTest.php in tests/src/Unit/UnpublishedNodesRedirectTest.php

File

src/Utils/UnpublishedNodesRedirectUtils.php, line 8

Namespace

Drupal\unpublished_nodes_redirect\Utils
View source
class UnpublishedNodesRedirectUtils {

  /**
   * Helper function to get node types on the site and allow them to be altered.
   *
   * @return array
   *   An array of node types.
   */
  public static function getNodeTypes() {

    // Get all the node types on the site.
    $node_types = \Drupal::entityTypeManager()
      ->getStorage('node_type')
      ->loadMultiple();
    $node_types_array = array_keys($node_types);

    // Allow other modules to override this.
    \Drupal::moduleHandler()
      ->alter('unpublished_nodes_redirect_node_types', $node_types_array);
    return $node_types_array;
  }

  /**
   * Gets the node type key used in this module.
   *
   * @param string $node_type
   *   Machine name of content type.
   *
   * @return string
   */
  public static function getNodeTypeKey($node_type) {
    return $node_type . '_unpublished_redirect_path';
  }

  /**
   * Gets the response code key used in this module.
   *
   * @param string $node_type
   *   Machine name of content type.
   *
   * @return string
   */
  public static function getResponseCodeKey($node_type) {
    return $node_type . '_unpublished_redirect_response_code';
  }

  /**
   * Checks that a node meets the criteria for a redirect.
   *
   * @param bool $node_status
   *   Published or Unpublished.
   * @param bool $is_published
   *   A boolean indicating if a user is anonymous.
   * @param string $redirect_path
   *   Path to be used for redirect.
   * @param string $response_code
   *   HTTP response code e.g 301.
   *
   * @return bool
   */
  public static function checksBeforeRedirect($is_published, $is_anonymous, $redirect_path, $response_code) {

    // Node is unpublished, user is not logged in and there is a redirect path
    // and response code.
    if (!$is_published && $is_anonymous && !empty($redirect_path) && !empty($response_code) && $response_code != 0) {
      return TRUE;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UnpublishedNodesRedirectUtils::checksBeforeRedirect public static function Checks that a node meets the criteria for a redirect.
UnpublishedNodesRedirectUtils::getNodeTypeKey public static function Gets the node type key used in this module.
UnpublishedNodesRedirectUtils::getNodeTypes public static function Helper function to get node types on the site and allow them to be altered.
UnpublishedNodesRedirectUtils::getResponseCodeKey public static function Gets the response code key used in this module.