class TypeStyleExtension in Type Style 8
A Twig extension to render type styles.
Hierarchy
- class \Drupal\type_style\TwigExtension\TypeStyleExtension extends \Drupal\type_style\TwigExtension\Twig_Extension
Expanded class hierarchy of TypeStyleExtension
1 string reference to 'TypeStyleExtension'
1 service uses TypeStyleExtension
File
- src/
TwigExtension/ TypeStyleExtension.php, line 11
Namespace
Drupal\type_style\TwigExtensionView source
class TypeStyleExtension extends \Twig_Extension {
/**
* Generates a list of all Twig functions that this extension defines.
*
* @return array
* A key/value array that defines custom Twig functions. The key denotes the
* function name used in the tag, e.g.:
* @code
* {{ testfunc() }}
* @endcode
*
* The value is a standard PHP callback that defines what the function does.
*/
public function getFunctions() {
return [
'type_style' => new TwigFunction('type_style', [
'Drupal\\type_style\\TwigExtension\\TypeStyleExtension',
'getTypeStyle',
]),
];
}
/**
* Gets a unique identifier for this Twig extension.
*
* @return string
* A unique identifier for this Twig extension.
*/
public function getName() {
return 'type_style.type_style_extension';
}
/**
* Helper function to grab a style for a given entity.
*
* @param string|\Drupal\core\Entity\EntityInterface $type
* The target entity type, or the entity object if available.
* @param mixed|string $id
* The target entity ID, or a style if an object is passed in $type.
* @param string $style
* The style name, or a default if an object is passed in $type.
* @param string $default
* A default value in case the style is not set. Defaults to empty string.
*
* @return string
* The style if set, or the default. This value is safe to use
*
* @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFunction()
*/
public static function getTypeStyle($type, $id, $style = NULL, $default = '') {
if ($type instanceof EntityInterface) {
return type_style_get_style($type, $id, $style);
}
elseif (($storage = \Drupal::entityTypeManager()
->getStorage($type)) && $style) {
if ($entity = $storage
->load($id)) {
return type_style_get_style($entity, $style, $default);
}
}
return $default;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TypeStyleExtension:: |
public | function | Generates a list of all Twig functions that this extension defines. | |
TypeStyleExtension:: |
public | function | Gets a unique identifier for this Twig extension. | |
TypeStyleExtension:: |
public static | function | Helper function to grab a style for a given entity. |