You are here

public static function Template::loadByTypeAndBundle in Wysiwyg API template plugin 3.0.x

Loads templates filtered by entity type and bundle.

Parameters

string $entity_type: (optional) The entity type to filter by. If this is not passed, only templates that specify *no* types will be returned.

string $bundle: (optional) The bundle for the given entity type to filter by.

Return value

\Drupal\wysiwyg_template_core\TemplateInterface[] The list of available templates filtered by entity type.

Overrides TemplateInterface::loadByTypeAndBundle

2 calls to Template::loadByTypeAndBundle()
TemplateController::listJson in src/Controller/TemplateController.php
A list of available templates based on the entity type and bundle.
wysiwyg_template_form_node_type_form_alter in ./wysiwyg_template.module
Implements hook_form_FORM_ID_alter().

File

src/Entity/Template.php, line 210

Class

Template
Defines the Template entity.

Namespace

Drupal\wysiwyg_template\Entity

Code

public static function loadByTypeAndBundle($entity_type, $bundle) : array {

  /** @var \Drupal\wysiwyg_template_core\TemplateInterface[] $templates */
  $templates = static::loadMultiple();
  foreach ($templates as $id => $template) {
    $bundles = $template
      ->getBundles($entity_type);
    if (!empty($bundles)) {
      if (!in_array($bundle, $bundles)) {

        // At least one bundle of the entity type is selected but not the given
        // one, so this template is not for us.
        unset($templates[$id]);
      }

      // Otherwise, we are relevant for the given bundle.
    }
    else {
      foreach ($template
        ->getBundles() as $type) {
        if ($type === $entity_type) {

          // Do not test the current type again.
          continue;
        }
        if (!empty($template
          ->getBundles($entity_type))) {

          // The template is selected for another entity type, so it's not for us.
          unset($templates[$id]);
          continue 2;
        }
      }
    }
  }
  return $templates;
}