function ElementInfoManager::fromRenderable

Creates a render object from a render array.

@template T of ElementInterface

Parameters

\Drupal\Core\Render\Element\ElementInterface|array $element: A render array or render objects. The latter is returned unchanged.

class-string<T> $class: The class of the render object being created.

Return value

T A render object.

Overrides ElementInfoManagerInterface::fromRenderable

File

core/lib/Drupal/Core/Render/ElementInfoManager.php, line 264

Class

ElementInfoManager
Provides a plugin manager for element plugins.

Namespace

Drupal\Core\Render

Code

public function fromRenderable(ElementInterface|array &$element, string $class = Generic::class) : ElementInterface {
  if ($element instanceof ElementInterface) {
    return $element;
  }
  if (isset($element['##object']) && $element['##object'] instanceof ElementInterface) {
    return $element['##object']->initializeInternalStorage($element);
  }
  $type = $element['#type'] ?? $this->getIdFromClass($class);
  if (!$type) {
    throw new \LogicException('The element passed to ElementInfoManager::fromRenderable must have a #type or a valid class must be provided.');
  }
  return $this->createInstance($type, element: $element);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.