function AttributeHelper::attributeExists

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Template/AttributeHelper.php \Drupal\Core\Template\AttributeHelper::attributeExists()
  2. 10 core/lib/Drupal/Core/Template/AttributeHelper.php \Drupal\Core\Template\AttributeHelper::attributeExists()
  3. 11.x core/lib/Drupal/Core/Template/AttributeHelper.php \Drupal\Core\Template\AttributeHelper::attributeExists()

Checks if the given attribute collection has an attribute.

Parameters

string $name: The name of the attribute to check for.

\Drupal\Core\Template\Attribute|array $collection: An Attribute object or an array of attributes.

Return value

bool TRUE if the attribute exists, FALSE otherwise.

Throws

\InvalidArgumentException When the input $collection is neither an Attribute object nor an array.

2 calls to AttributeHelper::attributeExists()
AttributeHelperTest::testAttributeExists in core/tests/Drupal/Tests/Core/Template/AttributeHelperTest.php
@covers ::attributeExists @dataProvider providerTestAttributeExists
template_preprocess_image in core/includes/theme.inc
Prepares variables for image templates.

File

core/lib/Drupal/Core/Template/AttributeHelper.php, line 34

Class

AttributeHelper
Helper class to deal with mixed array and Attribute operations.

Namespace

Drupal\Core\Template

Code

public static function attributeExists($name, $collection) {
    if ($collection instanceof Attribute) {
        return $collection->hasAttribute($name);
    }
    elseif (is_array($collection)) {
        return array_key_exists($name, $collection);
    }
    throw new \InvalidArgumentException('Invalid collection argument');
}

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