FlippingBookLinkFormatter.php in Flipping Book 8
File
src/Plugin/Field/FieldFormatter/FlippingBookLinkFormatter.php
View source
<?php
namespace Drupal\flipping_book\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\file\Plugin\Field\FieldType\FileItem;
class FlippingBookLinkFormatter extends FormatterBase {
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['url_only']['#access'] = TRUE;
return $elements;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = array();
$entity = $items
->getEntity();
foreach ($items as $delta => $item) {
$items[$delta]->title = $entity
->getName();
}
$settings = $this
->getSettings();
foreach ($items as $delta => $item) {
$url = $this
->buildUrl($item);
$link_title = $url
->toString();
if (empty($settings['url_only']) && !empty($item->title)) {
$link_title = \Drupal::token()
->replace($item->title, [
$entity
->getEntityTypeId() => $entity,
], [
'clear' => TRUE,
]);
}
if (!empty($settings['trim_length'])) {
$link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE);
}
if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
$element[$delta] = array(
'#plain_text' => $link_title,
);
if (!empty($item->_attributes)) {
$content = str_replace('internal:/', '', $item->uri);
$item->_attributes += array(
'content' => $content,
);
}
}
else {
$element[$delta] = array(
'#type' => 'link',
'#title' => $link_title,
'#options' => $url
->getOptions(),
);
$element[$delta]['#url'] = $url;
if (!empty($item->_attributes)) {
$element[$delta]['#options'] += array(
'attributes' => array(),
);
$element[$delta]['#options']['attributes'] += $item->_attributes;
unset($item->_attributes);
}
}
}
return $element;
}
protected function buildUrl(FileItem $item) {
$flipping_book = $item
->getRoot()
->getValue();
$url = \Drupal::service('flipping_book')
->buildFlippingBookUrl($flipping_book);
$settings = $this
->getSettings();
$options = $url
->getOptions();
if (!empty($settings['rel'])) {
$options['attributes']['rel'] = $settings['rel'];
}
if (!empty($settings['target'])) {
$options['attributes']['target'] = $settings['target'];
}
$url
->setOptions($options);
return $url;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return parent::isApplicable($field_definition) && $field_definition
->getTargetEntityTypeId() == 'flipping_book' && $field_definition
->getName() == 'file';
}
}