You are here

function JsResponse::isRenderable in JS Callback Handler 8.3

Helper method to determine if an array is renderable.

Parameters

array $data: The array to check.

bool $recursive: If TRUE, the entire array (children) is searched, otherwise only the first level is checked.

Return value

bool TRUE if renderable, FALSE if not.

2 calls to JsResponse::isRenderable()
JsResponse::render in src/JsResponse.php
Processes the content for output.
JsResponse::setContent in src/JsResponse.php

File

src/JsResponse.php, line 113

Class

JsResponse
JsResponse.

Namespace

Drupal\js

Code

function isRenderable($data = [], $recursive = TRUE) {

  // Immediately return if not an array.
  if (!is_array($data)) {
    return FALSE;
  }
  static $keys = [
    '#type',
    '#theme',
    '#theme_wrappers',
    '#children',
    '#markup',
    '#pre_render',
    '#post_render',
    '#lazy_builder',
    '#attached',
  ];
  if (array_intersect($keys, Element::properties($data))) {
    return TRUE;
  }
  if ($recursive) {

    // Cannot use \Drupal\Core\Render\Element::children here since that can
    // potentially trigger E_USER_ERROR if the array is invalid. Instead,
    // just filter the array by checking if the key is a "child" key.
    $children = array_filter(array_keys($data), '\\Drupal\\Core\\Render\\Element::child');
    foreach ($children as $child) {
      if (is_array($data[$child]) && $this
        ->isRenderable($data[$child])) {
        return TRUE;
      }
    }
  }
  return FALSE;
}