Skip to content
Snippets Groups Projects
Commit d77eacc1 authored by Karthikeyan Manivasagam's avatar Karthikeyan Manivasagam
Browse files

Added UUID display formatter

parent 279b2dfc
Branches
Tags 8.x-1.1-alpha2
No related merge requests found
......@@ -10,6 +10,8 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Uuid\Php;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Implements hook_help().
......@@ -92,3 +94,13 @@ function edit_uuid_form_validate(array $form, FormStateInterface $form_state) {
}
}
}
/**
* Implements hook_entity_base_field_info_alter().
*/
function edit_uuid_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
if (($field_name = $entity_type->getKey('uuid')) && $field = $fields[$field_name]) {
assert($field instanceof BaseFieldDefinition);
$field->setDisplayConfigurable('view', TRUE);
}
}
<?php
namespace Drupal\edit_uuid\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
/**
* Plugin implementation of the 'uuid' formatter.
*
* @FieldFormatter(
* id = "uuid",
* label = @Translation("UUID"),
* field_types = {
* "uuid",
* },
* )
*/
class EditUuidFieldFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$user = \Drupal::currentUser();
if($user->hasPermission('show edit_uuid')) {
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => $item->value,
];
}
}
return $elements;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment