You are here

protected function ThemeSuggestions::addSuggestionsForEntity in Express 8

Adds "bundle" and "view mode" suggestions for an entity.

This is a helper method because core's implementation of theme hook suggestions on entities is inconsistent.

@todo Remove/refactor once core issue is resolved.

Parameters

string $entity_type: Optional. A specific type of entity to look for.

string $prefix: Optional. A prefix (like "entity") to use. It will automatically be appended with the "__" separator.

See also

https://www.drupal.org/node/2808481

2 calls to ThemeSuggestions::addSuggestionsForEntity()
ThemeSuggestions::addEntitySuggestions in themes/contrib/bootstrap/src/Plugin/Alter/ThemeSuggestions.php
Adds "bundle" and "view mode" suggestions for an entity.
ThemeSuggestions::alterUser in themes/contrib/bootstrap/src/Plugin/Alter/ThemeSuggestions.php
Dynamic alter method for "user".

File

themes/contrib/bootstrap/src/Plugin/Alter/ThemeSuggestions.php, line 177
Contains \Drupal\bootstrap\Plugin\Alter\ThemeSuggestions.

Class

ThemeSuggestions
Implements hook_theme_suggestions_alter().

Namespace

Drupal\bootstrap\Plugin\Alter

Code

protected function addSuggestionsForEntity($entity_type = 'entity', $prefix = '') {

  // Immediately return if there is no element.
  if (!$this->element) {
    return;
  }

  // Extract the entity.
  if ($entity = $this
    ->getEntityObject($entity_type)) {
    $entity_type_id = $entity
      ->getEntityTypeId();
    $suggestions = [];

    // Only add the entity type identifier if there's a prefix.
    if (!empty($prefix)) {
      $prefix .= '__';
      $suggestions[] = $prefix . '__' . $entity_type_id;
    }

    // View mode.
    if ($view_mode = preg_replace('/[^A-Za-z0-9]+/', '_', $this->element
      ->getProperty('view_mode'))) {
      $suggestions[] = $prefix . $entity_type_id . '__' . $view_mode;

      // Bundle.
      if ($entity
        ->getEntityType()
        ->hasKey('bundle')) {
        $suggestions[] = $prefix . $entity_type_id . '__' . $entity
          ->bundle();
        $suggestions[] = $prefix . $entity_type_id . '__' . $entity
          ->bundle() . '__' . $view_mode;
      }
    }

    // Add suggestions.
    if ($suggestions) {
      $this
        ->addSuggestion($suggestions);
    }
  }
}