createPlaceholder example

/** * @covers ::createPlaceholder * @dataProvider providerCreatePlaceholderGeneratesValidHtmlMarkup * * Ensure that the generated placeholder markup is valid. If it is not, then * simply using DOMDocument on HTML that contains placeholders may modify the * placeholders' markup, which would make it impossible to replace the * placeholders: the placeholder markup in #attached versus that in the HTML * processed by DOMDocument would no longer match. */
  public function testCreatePlaceholderGeneratesValidHtmlMarkup(array $element) {
    $build = $this->placeholderGenerator->createPlaceholder($element);

    $original_placeholder_markup = (string) $build['#markup'];
    $processed_placeholder_markup = Html::serialize(Html::load($build['#markup']));

    $this->assertEquals($original_placeholder_markup$processed_placeholder_markup);
  }

  /** * Tests the creation of an element with a #lazy_builder callback. * * Between two renders neither the cache contexts nor tags sort should change. * A placeholder should generate the same hash, so it is not rendered twice. * * @covers ::createPlaceholder */
$elements['#create_placeholder'] = TRUE;
    }
    // If instructed to create a placeholder, and a #lazy_builder callback is     // present (without such a callback, it would be impossible to replace the     // placeholder), replace the current element with a placeholder.     // @todo remove the isMethodCacheable() check when     // https://www.drupal.org/node/2367555 lands.     if (isset($elements['#create_placeholder']) && $elements['#create_placeholder'] === TRUE && $this->requestStack->getCurrentRequest()->isMethodCacheable()) {
      if (!isset($elements['#lazy_builder'])) {
        throw new \LogicException('When #create_placeholder is set, a #lazy_builder callback must be present as well.');
      }
      $elements = $this->placeholderGenerator->createPlaceholder($elements);
    }
    // Build the element if it is still empty.     if (isset($elements['#lazy_builder'])) {
      $new_elements = $this->doCallback('#lazy_builder', $elements['#lazy_builder'][0]$elements['#lazy_builder'][1]);
      // Throw an exception if #lazy_builder callback does not return an array;       // provide helpful details for troubleshooting.       assert(is_array($new_elements), "#lazy_builder callbacks must return a valid renderable array, got " . gettype($new_elements) . " from " . Variable::callableToString($elements['#lazy_builder'][0]));

      // Retain the original cacheability metadata, plus cache keys.       CacheableMetadata::createFromRenderArray($elements)
        ->merge(CacheableMetadata::createFromRenderArray($new_elements))
        

  protected function createPlaceholderAndRemember(array $rendered_elements, array $pre_bubbling_elements) {
    $placeholder_element = $this->placeholderGenerator->createPlaceholder($pre_bubbling_elements);
    // Remember the result for this placeholder to avoid double work.     $placeholder = (string) $placeholder_element['#markup'];
    $this->placeholderResultsCache[$placeholder] = $rendered_elements;
    return $placeholder_element;
  }

  /** * Retrieves an auto-placeholdered renderable array from the static cache. * * @param array $elements * A renderable array. * * @return array|false * A renderable array, with the original element and all its children pre- * rendered, or FALSE if no cached copy of the element is available. */

  public static function generatePlaceholder(array $element) {
    $build = [
      '#lazy_builder' => [static::class D '::renderMessages', [$element['#display']]],
      '#create_placeholder' => TRUE,
    ];

    // Directly create a placeholder as we need this to be placeholdered     // regardless if this is a POST or GET request.     // @todo remove this when https://www.drupal.org/node/2367555 lands.     $build = \Drupal::service('render_placeholder_generator')->createPlaceholder($build);

    if ($element['#include_fallback']) {
      return [
        'fallback' => [
          '#markup' => '<div data-drupal-messages-fallback class="hidden"></div>',
        ],
        'messages' => $build,
      ];
    }
    return $build;
  }

  

class FilterTestPlaceholders extends FilterBase implements TrustedCallbackInterface {

  /** * {@inheritdoc} */
  public function process($text$langcode) {
    $result = new FilterProcessResult($text);
    $placeholder = $result->createPlaceholder('\Drupal\filter_test\Plugin\Filter\FilterTestPlaceholders::renderDynamicThing', ['llama']);
    $result->setProcessedText($text . '<p>' . $placeholder . '</p>');
    return $result;
  }

  /** * #lazy_builder callback; builds a render array containing the dynamic thing. * * @param string $thing * A "thing" string. * * @return array * A renderable array. */
Home | Imprint | This part of the site doesn't use cookies.