addClass example



  /** * Tests adding class attributes with the AttributeArray helper method. * @covers ::addClass */
  public function testAddClasses() {
    // Add empty Attribute object with no classes.     $attribute = new Attribute();

    // Add no class on empty attribute.     $attribute->addClass();
    $this->assertEmpty($attribute['class']);

    // Test various permutations of adding values to empty Attribute objects.     foreach ([NULL, FALSE, '', []] as $value) {
      // Single value.       $attribute->addClass($value);
      $this->assertEmpty((string) $attribute);

      // Multiple values.       $attribute->addClass($value$value);
      $this->assertEmpty((string) $attribute);

      
NULL,
    ];
  }

  /** * Tests Twig 'add_class' filter. * * @covers ::addClass * @dataProvider providerTestTwigAddClass */
  public function testTwigAddClass($element$classes$expected_result) {
    $processed = $this->systemUnderTest->addClass($element$classes);
    $this->assertEquals($expected_result$processed);
  }

  /** * Provides data for ::testTwigAddClass(). * * @return \Iterator */
  public function providerTestTwigAddClass(): \Iterator {
    yield 'should add a class on element' => [
      ['#type' => 'container'],
      

  public function addClass(array $element, ...$classes): array {
    $attributes = new Attribute($element['#attributes'] ?? []);
    $attributes->addClass(...$classes);
    $element['#attributes'] = $attributes->toArray();

    // Make sure element gets rendered again.     unset($element['#printed']);

    return $element;
  }

  /** * Sets an attribute on a given element. * * Assumes the element is an array. * * @param array $element * A render element. * @param string $name * The attribute name. * @param mixed $value * (optional) The attribute value. * * @return array * The element with the given sanitized attribute's value. */
Home | Imprint | This part of the site doesn't use cookies.