You are here

public static function Template::loadByNodeType in Wysiwyg API template plugin 8.2

Same name and namespace in other branches
  1. 3.0.x 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

3 calls to Template::loadByNodeType()
TemplateController::listJson in src/Controller/TemplateController.php
A list of available templates based on the content type.
TemplateTest::testInterfaceMethods in tests/src/Kernel/Entity/TemplateTest.php
@covers ::id @covers ::label @covers ::getBody @covers ::getFormat @covers ::getNodeTypes @covers ::getWeight @covers ::loadByNodeType
wysiwyg_template_form_node_type_form_alter in ./wysiwyg_template.module
Implements hook_form_FORM_ID_alter().

File

src/Entity/Template.php, line 146

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;
}