You are here

LdapVariableImageAttribute.php in Lightweight Directory Access Protocol (LDAP) 8.3

Same filename and directory in other branches
  1. 8.4 ldap_query/src/Plugin/views/field/LdapVariableImageAttribute.php

File

ldap_query/src/Plugin/views/field/LdapVariableImageAttribute.php
View source
<?php

namespace Drupal\ldap_query\Plugin\views\field;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;

/**
 * Let's the user choose which LDAP attribute to use from the query.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("ldap_variable_image_attribute")
 */
class LdapVariableImageAttribute extends LdapVariableAttribute {

  /**
   * Encodes a binary image for display directly in Views.
   *
   * @param \Drupal\views\ResultRow $values
   *   Result row.
   *
   * @return array|null
   *   Markup with image if available.
   */
  public function render(ResultRow $values) {
    if ($this
      ->getValue($values)) {
      $data = $this
        ->getValue($values)[0];
      $formatter = new FormattableMarkup('<img src="data:image/jpeg;base64,:src"/>', [
        ':src' => base64_encode($data),
      ]);
      return [
        '#markup' => $formatter,
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);

    // To avoid code complexity, multi-value is removed for images, since
    // that is in unusual scenario.
    unset($form['multi_value']);
    unset($form['value_separator']);
    unset($form['index_value']);
  }

}

Classes

Namesort descending Description
LdapVariableImageAttribute Let's the user choose which LDAP attribute to use from the query.