Verified Commit 72ab7783 authored by Dave Long's avatar Dave Long
Browse files

task: #2258355 Deprecate hide() and show()

By: marcingy
By: alexpott
By: Arnion
By: andypost
By: MerryHamster
By: karishmaamin
By: longwave
By: nnevill
By: voleger
By: nod_
By: bhanu951
By: spokje
By: xjm
By: smustgrave
By: catch
By: vladimiraus
By: bnjmnm
(cherry picked from commit afe536a7)
parent 014ef571
Loading
Loading
Loading
Loading
Loading
+18 −25
Original line number Diff line number Diff line
@@ -290,14 +290,8 @@ function drupal_attach_tabledrag(&$element, array $options): void {
/**
 * Hides an element from later rendering.
 *
 * The first time render() or RenderInterface::render() is called on an element
 * tree, as each element in the tree is rendered, it is marked with a #printed
 * flag and the rendered children of the element are cached. Subsequent calls to
 * render() or RenderInterface::render() will not traverse the child tree of
 * this element again: they will just use the cached children. So if you want to
 * hide an element, be sure to call hide() on the element before its parent tree
 * is rendered for the first time, as it will have no effect on subsequent
 * renderings of the parent tree.
 * Refer to \Drupal\Core\Render\RendererInterface::render()
 * for additional documentation.
 *
 * @param array $element
 *   The element to be hidden.
@@ -305,11 +299,15 @@ function drupal_attach_tabledrag(&$element, array $options): void {
 * @return array
 *   The element.
 *
 * @see \Drupal\Core\Render\RendererInterface
 * @see render()
 * @see show()
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. To hide form
 *   elements, use ['#access'] = FALSE. For render elements, use
 *   ['#printed'] = TRUE.
 *
 * @see https://www.drupal.org/node/3261271
 * @see \Drupal\Core\Render\RendererInterface::render()
 */
function hide(&$element) {
  @trigger_error("The global hide() function is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. To hide form elements, use ['#access'] = FALSE. For render elements, use ['#printed'] = TRUE. See https://www.drupal.org/node/3261271", E_USER_DEPRECATED);
  $element['#printed'] = TRUE;
  return $element;
}
@@ -317,17 +315,8 @@ function hide(&$element) {
/**
 * Shows a hidden element for later rendering.
 *
 * You can also use render($element), which shows the element while rendering
 * it.
 *
 * The first time render() or RenderInterface::render() is called on an element
 * tree, as each element in the tree is rendered, it is marked with a #printed
 * flag and the rendered children of the element are cached. Subsequent calls to
 * render() or RenderInterface::render() will not traverse the child tree of
 * this element again: they will just use the cached children. So if you want to
 * show an element, be sure to call show() on the element before its parent tree
 * is rendered for the first time, as it will have no effect on subsequent
 * renderings of the parent tree.
 * Refer to \Drupal\Core\Render\RendererInterface::render()
 * for additional documentation.
 *
 * @param array $element
 *   The element to be shown.
@@ -335,11 +324,15 @@ function hide(&$element) {
 * @return array
 *   The element.
 *
 * @see \Drupal\Core\Render\RendererInterface
 * @see render()
 * @see hide()
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. To show form
 *   elements, use ['#access'] = TRUE. For render elements, use
 *   ['#printed'] = FALSE.
 *
 * @see https://www.drupal.org/node/3261271
 * @see \Drupal\Core\Render\RendererInterface::render()
 */
function show(&$element) {
  @trigger_error("The global show() function is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. To show form elements, use ['#access'] = TRUE. For render elements, use ['#printed'] = FALSE. See https://www.drupal.org/node/3261271", E_USER_DEPRECATED);
  $element['#printed'] = FALSE;
  return $element;
}
+9 −0
Original line number Diff line number Diff line
@@ -124,6 +124,15 @@ public function renderPlaceholder($placeholder, array $elements);
   * and this must also bubble from child to parent. Therefore,
   * \Drupal\Core\Render\BubbleableMetadata includes that data as well.
   *
   * The first time RendererInterface::render() is called on an element tree,
   * as each element in the tree is rendered, it is marked with a #printed flag
   * and the rendered children of the element are cached. Subsequent calls to
   * RendererInterface::render() will not traverse the child trees of elements
   * again: they will just use the cached children. So if you want to hide an
   * element, be sure to set element['#printed'] = TRUE before its parent
   * tree is rendered for the first time, as it will have no effect on
   * subsequent renderings of the parent tree.
   *
   * The process of rendering an element is recursive unless the element defines
   * an implemented theme hook in #theme. During each call to
   * Renderer::render(), the outermost renderable array (also known as an
+6 −5
Original line number Diff line number Diff line
@@ -261,7 +261,7 @@ public function preprocessFileWidgetMultiple(array &$variables): void {
      $operations_elements = [];
      foreach (Element::children($widget) as $key) {
        if (isset($widget[$key]['#type']) && $widget[$key]['#type'] == 'submit') {
          hide($widget[$key]);
          $widget[$key]['#printed'] = TRUE;
          $operations_elements[] = &$widget[$key];
        }
      }
@@ -269,9 +269,9 @@ public function preprocessFileWidgetMultiple(array &$variables): void {
      // Delay rendering of the "Display" option and the weight selector, so
      // that each can be rendered later in its own column.
      if ($element['#display_field']) {
        hide($widget['display']);
        $widget['display']['#printed'] = TRUE;
      }
      hide($widget['_weight']);
      $widget['_weight']['#printed'] = TRUE;
      $widget['_weight']['#attributes']['class'] = [$weight_class];

      // Render everything else together in a column, without the normal
@@ -293,9 +293,10 @@ public function preprocessFileWidgetMultiple(array &$variables): void {
      ];

      // Show the buttons that had previously been marked as hidden in this
      // preprocess function. We use show() to undo the earlier hide().
      // preprocess function. We use ['#printed'] = FALSE to undo the earlier
      // ['#printed'] = TRUE.
      foreach (Element::children($operations_elements) as $key) {
        show($operations_elements[$key]);
        $operations_elements[$key]['#printed'] = FALSE;
      }
      $row[] = [
        'data' => $operations_elements,
+47 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\Core\Render\Element;

use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests the deprecation of global rendering functions.
 */
#[Group("Render")]
#[Group("legacy")]
#[RunTestsInSeparateProcesses]
#[IgnoreDeprecations]
class ProceduralApiDeprecationTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  public function setUp(): void {
    parent::setUp();
    include_once $this->root . '/core/includes/common.inc';
  }

  /**
   * Tests the deprecation of the global hide() function.
   */
  public function testHideDeprecation(): void {
    $element = [];
    $this->expectUserDeprecationMessage("The global hide() function is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. To hide form elements, use ['#access'] = FALSE. For render elements, use ['#printed'] = TRUE. See https://www.drupal.org/node/3261271");
    $this->assertEquals(['#printed' => TRUE], hide($element));
  }

  /**
   * Tests the deprecation of the global show() function.
   */
  public function testShowDeprecation(): void {
    $element = [];
    $this->expectUserDeprecationMessage("The global show() function is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. To show form elements, use ['#access'] = TRUE. For render elements, use ['#printed'] = FALSE. See https://www.drupal.org/node/3261271");
    $this->assertEquals(['#printed' => FALSE], show($element));
  }

}