You are here

public function ParagraphBlocksEntityManager::getEntity in Paragraph blocks 8

Same name and namespace in other branches
  1. 8.2 src/ParagraphBlocksEntityManager.php \Drupal\paragraph_blocks\ParagraphBlocksEntityManager::getEntity()
  2. 3.x src/ParagraphBlocksEntityManager.php \Drupal\paragraph_blocks\ParagraphBlocksEntityManager::getEntity()

Return the entity of the path.

@todo: This implements something similar to menu_get_object() which was likely removed intentionally, so is this a bad idea?

Parameters

string $path: (optional) The path to lookup, defaults to the current path.

Return value

\Drupal\Core\Entity\EntityInterface|null The entity of the current path, or null if none.

See also

entity_css_get_entity()

1 call to ParagraphBlocksEntityManager::getEntity()
ParagraphBlocksEntityManager::getRefererEntity in src/ParagraphBlocksEntityManager.php
Return the entity of the referer, useful when called via AJAX.

File

src/ParagraphBlocksEntityManager.php, line 82

Class

ParagraphBlocksEntityManager
Class HcpCoreManager.

Namespace

Drupal\paragraph_blocks

Code

public function getEntity($path = NULL) {

  // Get the current path if none is specified.
  if (!$path) {
    $path = parse_url(\Drupal::requestStack()
      ->getCurrentRequest()
      ->getRequestUri(), PHP_URL_PATH);
  }
  $base_path = base_path();
  if (empty($path) || $path == $base_path || $path == '<front>') {
    $path = \Drupal::config('system.site')
      ->get('page.front');
  }

  // Convert the specified path to an internal path.
  $source_path = \Drupal::service('path.alias_manager')
    ->getPathByAlias($path);
  $source_path = preg_replace(":^{$base_path}:", '', $source_path);

  // Check if this is the entity path.
  // @todo: this makes assumption that are possibly not valid. For example, is
  // the entity path always "/entity_type_id/entity_id"?
  $parts = explode('/', trim($source_path, '/'));
  if (count($parts) >= 2) {
    list($entity_type_id, $entity_id) = $parts;
    $entity_type_id = str_replace('-', '_', $entity_type_id);

    // Check that the path is for an entity.
    $entity_type_manager = \Drupal::entityTypeManager();

    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_types = $entity_type_manager
      ->getDefinitions();
    if (is_numeric($entity_id) && isset($entity_types[$entity_type_id])) {

      /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
      $entity_storage = $entity_type_manager
        ->getStorage($entity_type_id);
      if ($entity_storage) {

        // Load and return the entity.
        return $entity_storage
          ->load($entity_id);
      }
    }
  }
  return NULL;
}