improvements-2.x-dev/src/Plugin/Field/FieldFormatter/RequestInfoFormatter.php

src/Plugin/Field/FieldFormatter/RequestInfoFormatter.php
<?php

namespace Drupal\improvements\Plugin\Field\FieldFormatter;

use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\druhels\ArrayHelper;
use Drupal\improvements\Plugin\Field\FieldType\RequestInfoItem;

/**
 * @FieldFormatter(
 *   id = "request_info",
 *   label = @Translation("Request info"),
 *   field_types = {
 *     "request_info",
 *   },
 * )
 */
class RequestInfoFormatter extends FormatterBase {

  /**
   * {@inheritDoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode): array|string {
    if (!$items->isEmpty()) {
      $item = $items->first(); /** @var RequestInfoItem $item */

      $data = [
        'IP' => Html::escape($item->ip),
        'Host' => Html::escape($item->host),
        'User-Agent' => Html::escape($item->useragent),
        'Referer' => Html::escape($item->referer),
      ];

      return [['#markup' => ArrayHelper::formatArrayAsKeyValueList($data, ': ', '<br />')]];
    }

    return [];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc