Skip to content
Snippets Groups Projects
Commit afeeffe6 authored by Adam Shepherd's avatar Adam Shepherd
Browse files

Issue #3104204 by AdamPS: Add submitted formatter setting for user view mode

parent f1b27fee
No related branches found
No related tags found
No related merge requests found
......@@ -107,6 +107,7 @@ function manage_display_theme() {
'base hook' => 'username',
'variables' => [
'date' => NULL,
'user_picture' => NULL,
'metadata' => [],
'account' => NULL,
'attributes' => [],
......@@ -120,18 +121,6 @@ function manage_display_theme() {
* Prepares variables for 'submitted' field formatter template.
*/
function template_preprocess_submitted(&$variables) {
$account = $variables['account'];
// @todo Don't use the theme setting, instead put a setting in the formatter
// to choose a display mode (don't hard-code to 'compact').
// Hence hide the GUI for features.node_user_picture.
if (theme_get_setting('features.node_user_picture')) {
// To change user picture settings (e.g. image style), edit the 'compact'
// view mode on the User entity. Note that the 'compact' view mode might
// not be configured, so remember to always check the theme setting first.
$variables['author_picture'] = user_view($account, 'compact');
}
$variables['date'] = \Drupal::service('date.formatter')->format($variables['date']);
}
......@@ -142,3 +131,13 @@ function manage_display_form_node_type_form_alter(&$form, FormStateInterface $fo
// Hide display_submitted in GUI.
$form['display']['#access'] = FALSE;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function manage_display_form_system_theme_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
// Hide the theme settings for user_pictures because the submitted formatter
// has its own setting.
$form['theme_settings']['toggle_node_user_picture']['#access'] = FALSE;
//$form['theme_settings']['toggle_comment_user_picture']['#access'] = FALSE;
}
......@@ -2,8 +2,12 @@
namespace Drupal\manage_display\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Plugin\Field\FieldFormatter\AuthorFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* A field formatter for entity titles.
......@@ -18,15 +22,110 @@ use Drupal\user\Plugin\Field\FieldFormatter\AuthorFormatter;
*/
class SubmittedFormatter extends AuthorFormatter {
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* Constructs a EntityReferenceEntityFormatter instance.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings settings.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityDisplayRepositoryInterface $entity_display_repository) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->entityDisplayRepository = $entity_display_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('entity_display.repository')
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$options = parent::defaultSettings();
$options['user_picture'] = '';
return $options;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$view_modes = ['' => $this->t('- None -')];
$view_modes += $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type'));
$form['user_picture'] = [
'#type' => 'select',
'#options' => $view_modes,
'#title' => $this->t('View mode for user picture'),
'#default_value' => $this->getSetting('user_picture'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
if ($view_mode = $this->getSetting('user_picture')) {
$view_modes = $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type'));
$summary[] = t('User picture view mode: @mode', ['@mode' => $view_modes[$view_mode] ?? $view_mode]);
}
else {
$summary[] = t('No user picture');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
foreach ($elements as $delta => $entity) {
$elements[$delta]['#theme'] = 'submitted';
$elements[$delta]['#date'] = $items->getEntity()->get('created')->value ?? NULL;
foreach ($elements as &$element) {
$element['#theme'] = 'submitted';
$element['#date'] = $items->getEntity()->get('created')->value ?? NULL;
if ($view_mode = $this->getSetting('user_picture')) {
$element['#user_picture'] = user_view($element['#account'], $view_mode);
}
}
return $elements;
......
......@@ -8,8 +8,7 @@
* contents of username.html.twig.
*
* Available variables:
* - author_picture: The author user entity, rendered using the "compact"
* view mode.
* - user_picture: The rendered author user entity
* - metadata: Metadata for this entity.
* - date: Creation date field.
* - author_attributes: HTML attributes for the containing element.
......@@ -34,7 +33,7 @@
*/
#}
<footer>
{{ author_picture }}
{{ user_picture }}
<div{{ author_attributes }}>
{% if link_path -%}
{% if date -%}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment