class UIkitComment in UIkit Components 8.3
Provides a render element for the Comment component.
Properties:
- #avatar: An assocative array containing:
- style_name: The name of the image style to be applied.
- uri: URI of the source image before styling.
- height: The height of the image.
- width: The width of the image.
- alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0 always require an alt attribute. The HTML 5 draft allows the alt attribute to be omitted in some cases. Therefore, this variable defaults to an empty string, but can be set to NULL for the attribute to be omitted. Usually, neither omission nor an empty string satisfies accessibility requirements, so it is strongly encouraged for code using '#theme' => 'image_style' to pass a meaningful value for this variable.
- title: The title text is displayed when the image is hovered in some popular browsers.
- attributes: Associative array of attributes to be placed in the img tag.
- #title: The title to display in the comment header.
- #meta: An array containing the metadata to display in the comment header.
- #comment: The content to display for the comment body.
- #primary: Boolean indicating whether to style a comment differently, for example to highlight it as the admin's comment.
Usage example:
$build['comment'] = [
'#type' => 'uikit_comment',
'#avatar' => [
'style_name' => 'thumbnail',
'uri' => 'public://avatar.jpg',
'height' => '80',
'width' => '80',
'alt' => t('Avatar'),
'title' => t('Author'),
],
'#title' => Markup::create('<a class="uk-link-reset" href="#">Author</a>'),
'#meta' => [
Markup::create('<a href="#">12 days ago</a>'),
Markup::create('<a href="#">Reply</a>'),
],
'#comment' => Markup::create('<p>Lorem ipsum dolor sit amet.</p>'),
'#primary' => TRUE,
];
Plugin annotation
@RenderElement("uikit_comment");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\uikit_components\Element\UIkitComment
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of UIkitComment
See also
template_preprocess_image_style()
template_preprocess_uikit_comment()
https://getuikit.com/docs/comment
Related topics
File
- src/
Element/ UIkitComment.php, line 67
Namespace
Drupal\uikit_components\ElementView source
class UIkitComment extends RenderElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#avatar' => [
'style_name' => NULL,
'uri' => NULL,
],
'#title' => NULL,
'#meta' => NULL,
'#comment' => NULL,
'#primary' => FALSE,
'#attributes' => new Attribute(),
'#pre_render' => [
[
$class,
'preRenderUIkitComment',
],
],
'#theme_wrappers' => [
'uikit_comment',
],
];
}
/**
* Pre-render callback: Sets the comment attributes.
*
* Doing so during pre_render gives modules a chance to alter the comment.
*
* @param array $element
* A renderable array.
*
* @return array
* A renderable array.
*/
public static function preRenderUIkitComment($element) {
// Set the attributes for the comment outer element.
$element['#attributes']
->addClass('uk-comment');
if ($element['#primary']) {
$element['#attributes']
->addClass('uk-comment-primary');
}
// Set the variables for the avatar so it can be rendered as an image style.
if (!empty($element['#avatar'])) {
$avatar = $element['#avatar'];
// Check if the file exists before continuing.
if (file_exists($avatar['uri'])) {
// Set the #avatar variable to render the image using the given image
// style.
$managed_file = ImageStyleRenderer::loadImageManagedFile($avatar);
if ($managed_file) {
// First check if this is a managed file and set the #avatar variable
// using our image style rendering class.
$element['#avatar'] = $managed_file;
}
else {
// Otherwise build the avatar using a simpler method, with less
// information being added to the #avatar variable.
$element['#avatar'] = ImageStyleRenderer::loadImageFile($avatar);
}
// Set the attributes to the avatar.
$element['#avatar']['#attributes'] = new Attribute();
$element['#avatar']['#attributes']
->addClass('uk-comment-avatar');
// Recursively merge the user-defined attributes with the avatar
// attributes, if the user assigned additional attributes.
if (isset($avatar['attributes'])) {
$element['#avatar']['#attributes'] = array_merge_recursive($element['#avatar']['#attributes'], $avatar['attributes']);
}
// Add the alt, title, height and width attributes, if they are set.
if (isset($avatar['alt'])) {
$element['#avatar']['#alt'] = $avatar['alt'];
}
if (isset($avatar['title'])) {
$element['#avatar']['#title'] = $avatar['title'];
}
if (isset($avatar['height'])) {
$element['#avatar']['#height'] = $avatar['height'];
}
if (isset($avatar['width'])) {
$element['#avatar']['#width'] = $avatar['width'];
}
}
else {
$element['#avatar'] = [];
}
}
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UIkitComment:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
UIkitComment:: |
public static | function | Pre-render callback: Sets the comment attributes. |