Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatter setting to suppress MARC Relator code in () #67

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Plugin/Field/FieldFormatter/TypedRelationFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
* Plugin implementation of the 'TypedRelationFormatter'.
Expand All @@ -18,6 +19,38 @@
*/
class TypedRelationFormatter extends EntityReferenceLabelFormatter {

/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'suppress_code' => FALSE,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {

$elements = parent::settingsForm($form, $form_state);
$elements['suppress_code'] = [
'#title' => t('Hide code in () after label, if present'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('suppress_code'),
];

return $elements;
}

/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this->getSetting('suppress_code') ? t('Suppress code in (), if present') : t('Show code in (), if present');
return $summary;
}

/**
* {@inheritdoc}
*/
Expand All @@ -28,9 +61,13 @@ public function viewElements(FieldItemListInterface $items, $langcode) {

$rel_types = $item->getRelTypes();
$rel_type = isset($rel_types[$item->rel_type]) ? $rel_types[$item->rel_type] : $item->rel_type;
if (!empty($rel_type)) {
$elements[$delta]['#prefix'] = $rel_type . ': ';

// Suppress code, e.g. change Author (aut) to Author.
if ($this->getSetting('suppress_code') == TRUE) {
$rel_type = preg_replace('/ \([^()]*\)/', '', $rel_type);
}

$elements[$delta]['#prefix'] = $rel_type . ': ';
}

return $elements;
Expand Down