Browse Source

added orcid field formatter

main
Paul Pound 2 months ago
parent
commit
dfab991efd
  1. 52
      src/Plugin/Field/FieldFormatter/OrcidFieldFormatter.php

52
src/Plugin/Field/FieldFormatter/OrcidFieldFormatter.php

@ -0,0 +1,52 @@
<?php
namespace Drupal\doi_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* Plugin implementation of the 'Facet_Field_Formatter' formatter.
*
* @FieldFormatter(
* id = "orcid_text_field_formatter",
* module = "doi_field_formatter",
* label = @Translation("Orcid Field Formatter"),
* description = @Translation("Create links to ORCID resources"),
* field_types = {
* "text",
* "string"
* }
* )
*/
class OrcidFieldFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = $this->t('Format string or text fields as as ORCID links.');
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
$prefix = 'https://orcid.org/';
// Assumes all field values are just plain text or strings values and not full orcid URLS.
$orcidUrl = $prefix . $item->value;
$imgSrc = '<img class="orcid-id " src="https://orcid.org/sites/default/files/images/orcid_24x24.png">';
$link = Link::fromTextAndUrl($orcidUrl, Url::fromUri($orcidUrl));
$element[$delta] = [$link->toRenderable()];
$element[$delta][0]['#prefix'] = $imgSrc;
}
return $element;
}
}
Loading…
Cancel
Save