You are here

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

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\Element
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UIkitComment::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
UIkitComment::preRenderUIkitComment public static function Pre-render callback: Sets the comment attributes.