home article cards links made active

This commit is contained in:
2021-06-10 17:42:55 +02:00
parent 352223500f
commit 27ece7a12c
10 changed files with 353 additions and 23 deletions

View File

@@ -0,0 +1,105 @@
<?php
namespace Drupal\vue_link_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\vue_link_formatter\Plugin\Field\FieldFormatter\VueImageFormatter;
/**
* Plugin implementation of the 'image_delta_formatter' formatter.
*
* @FieldFormatter(
* id = "vue_image_delta_formatter",
* label = @Translation("Vue Image delta"),
* description = @Translation("Display specific deltas of an image field with vue atributes."),
* field_types = {
* "image"
* }
* )
*/
class VueImageDeltaFormatter extends VueImageFormatter {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'deltas' => 0,
'deltas_reversed' => FALSE,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['deltas'] = [
'#type' => 'textfield',
'#title' => $this->t('Delta'),
'#description' => $this->t('Enter a delta, or a comma-separated list of deltas that should be shown. For example: 0, 1, 4.'),
'#size' => 10,
'#default_value' => $this->getSetting('deltas'),
'#required' => TRUE,
'#weight' => -20,
];
$element['deltas_reversed'] = [
'#title' => $this->t('Reversed'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('deltas_reversed'),
'#description' => $this->t('Start from the last values.'),
'#weight' => -10,
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$settings = $this->getSettings();
$summary = parent::settingsSummary();
$count = count(explode(',', $settings['deltas']));
$args = [
'@deltas' => trim($settings['deltas']),
];
$delta_summary = empty($settings['deltas_reversed']) ? $this->formatPlural($count, 'Delta: @deltas', 'Deltas: @deltas', $args) : $this->formatPlural($count, 'Delta: @deltas (reversed, no effect).', 'Deltas: @deltas (reversed).', $args);
$summary[] = $delta_summary;
return $summary;
}
/**
* {@inheritdoc}
*/
protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
$files = parent::getEntitiesToView($items, $langcode);
// Prepare an array of selected deltas from the entered string.
if (mb_strpos($this->getSetting('deltas'), ',')) {
$deltas = explode(',', $this->getSetting('deltas'));
$deltas = array_map('trim', $deltas);
}
else {
$delta = trim($this->getSetting('deltas'));
$deltas = [$delta];
}
foreach (array_keys($files) as $delta) {
if (!in_array($delta, $deltas)) {
unset($files[$delta]);
}
}
// Reverse the items if needed.
if ($this->getSetting('deltas_reversed')) {
$files = array_reverse($files);
}
return $files;
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Drupal\vue_link_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
/**
* Plugin implementation of the 'image' formatter with vuejs attributes.
*
* @FieldFormatter(
* id = "vueimage",
* label = @Translation("Vue Image"),
* field_types = {
* "image"
* },
* quickedit = {
* "editor" = "image"
* }
* )
*/
class VueImageFormatter extends ImageFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($files)) {
return $elements;
}
$url = NULL;
$image_link_setting = $this->getSetting('image_link');
// Check if the formatter involves a link.
if ($image_link_setting == 'content') {
$entity = $items->getEntity();
if (!$entity->isNew()) {
$url = $entity->toUrl();
$url->setOption('attributes', array(
'@click.prevent' => 'onClickLink',
'data-id' => $entity->id(),
'data-entity-type' => $entity->getEntityTypeId(),
'data-bundle' => $entity->bundle(),
));
}
}
elseif ($image_link_setting == 'file') {
$link_file = TRUE;
}
$image_style_setting = $this->getSetting('image_style');
// Collect cache tags to be added for each item in the field.
$base_cache_tags = [];
if (!empty($image_style_setting)) {
$image_style = $this->imageStyleStorage->load($image_style_setting);
$base_cache_tags = $image_style->getCacheTags();
}
foreach ($files as $delta => $file) {
$cache_contexts = [];
if (isset($link_file)) {
$image_uri = $file->getFileUri();
// @todo Wrap in file_url_transform_relative(). This is currently
// impossible. As a work-around, we currently add the 'url.site' cache
// context to ensure different file URLs are generated for different
// sites in a multisite setup, including HTTP and HTTPS versions of the
// same site. Fix in https://www.drupal.org/node/2646744.
$url = Url::fromUri(file_create_url($image_uri));
$cache_contexts[] = 'url.site';
}
$cache_tags = Cache::mergeTags($base_cache_tags, $file->getCacheTags());
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item = $file->_referringItem;
$item_attributes = $item->_attributes;
unset($item->_attributes);
$elements[$delta] = [
'#theme' => 'image_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $image_style_setting,
'#url' => $url,
'#cache' => [
'tags' => $cache_tags,
'contexts' => $cache_contexts,
],
];
}
return $elements;
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Drupal\vue_link_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'string' formatter + @click vue attribute
*
* @FieldFormatter(
* id = "vuestring",
* label = @Translation("Vue link Plain text"),
* field_types = {
* "string",
* "uri",
* },
* quickedit = {
* "editor" = "plain_text"
* }
* )
*/
class VueStringFormatter extends StringFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$url = NULL;
if ($this->getSetting('link_to_entity')) {
$entity = $items->getEntity();
$url = $this->getEntityUrl($entity);
}
foreach ($items as $delta => $item) {
$view_value = $this->viewValue($item);
if ($url) {
$elements[$delta] = [
'#type' => 'link',
'#title' => $view_value,
'#url' => $url,
'#attributes' => array(
'@click.prevent' => 'onClickLink',
'data-id' => $entity->id(),
'data-entity-type' => $entity->getEntityTypeId(),
'data-bundle' => $entity->bundle(),
// 'data-bundle' => $entity
// 'nid' => $entity->nid(),
// 'type' => $entity->getType()
)
];
}
else {
$elements[$delta] = $view_value;
}
}
return $elements;
}
}