You are here

public static function UIkitComment::preRenderUIkitComment in UIkit Components 8.3

Pre-render callback: Sets the comment attributes.

Doing so during pre_render gives modules a chance to alter the comment.

Parameters

array $element: A renderable array.

Return value

array A renderable array.

File

src/Element/UIkitComment.php, line 102

Class

UIkitComment
Provides a render element for the Comment component.

Namespace

Drupal\uikit_components\Element

Code

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;
}