SocialAlbumEntityReferenceLabelFormatter.php in Open Social 10.2.x
File
modules/social_features/social_album/src/Plugin/Field/FieldFormatter/SocialAlbumEntityReferenceLabelFormatter.php
View source
<?php
namespace Drupal\social_album\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
use Drupal\Core\Link;
class SocialAlbumEntityReferenceLabelFormatter extends EntityReferenceLabelFormatter {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
foreach ($items as $delta => $item) {
if (!isset($elements[$delta])) {
continue;
}
$cache = $elements[$delta]['#cache'];
if (isset($elements[$delta]['#title'])) {
$elements[$delta] = [
'#markup' => $this
->t('Images posted in album %album', [
'%album' => Link::fromTextAndUrl($elements[$delta]['#title'], $elements[$delta]['#url'])
->toString(),
]),
];
}
else {
$elements[$delta] = [
'#plain_text' => $this
->t('Images posted in album %album', [
'%album' => $elements[$delta]['#plain_text'],
]),
];
}
$elements[$delta]['#cache'] = $cache;
}
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$settings = $field_definition
->getSettings();
return $settings['target_type'] === 'node' && $settings['handler'] === 'views' && $settings['handler_settings']['view']['view_name'] === 'albums';
}
}