You are here

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

Same name and namespace in other branches
  1. 8.2 src/Entity/Template.php \Drupal\wysiwyg_template\Entity\Template::loadByNodeType()

Loads templates filtered by node type.

Parameters

\Drupal\node\NodeTypeInterface $node_type: (optional) The node type to filter by. If this is not passed, only templates that specify *no* types will be returned.

Return value

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

Overrides TemplateInterface::loadByNodeType

1 call to Template::loadByNodeType()
TemplateTest::testInterfaceMethods in tests/src/Kernel/Entity/TemplateTest.php
@covers ::id @covers ::label @covers ::getBody @covers ::getFormat @covers ::getNodeTypes @covers ::getWeight @covers ::loadByNodeType

File

src/Entity/Template.php, line 184

Class

Template
Defines the Template entity.

Namespace

Drupal\wysiwyg_template\Entity

Code

public static function loadByNodeType(NodeTypeInterface $node_type = NULL) : array {

  /** @var \Drupal\wysiwyg_template_core\TemplateInterface[] $templates */
  $templates = static::loadMultiple();
  foreach ($templates as $id => $template) {
    if (!$node_type) {

      // If no node type is passed than all templates that *don't specify any*
      // types are included, but those specifying a type are not.
      if (!empty($template
        ->getNodeTypes())) {
        unset($templates[$id]);
      }
    }
    else {

      // Any templates without types, plus the templates that specify this type.
      if (empty($template
        ->getNodeTypes()) || in_array($node_type
        ->id(), $template
        ->getNodeTypes(), TRUE)) {
        continue;
      }
      unset($templates[$id]);
    }
  }
  return $templates;
}