Loading manage_display.module +11 −12 Changes for manage_display.module: 11 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -107,6 +107,7 @@ function manage_display_theme() { 'base hook' => 'username', 'variables' => [ 'date' => NULL, 'user_picture' => NULL, 'metadata' => [], 'account' => NULL, 'attributes' => [], Loading @@ -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']); } Loading @@ -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; } src/Plugin/Field/FieldFormatter/SubmittedFormatter.php +102 −3 Changes for src/Plugin/Field/FieldFormatter/SubmittedFormatter.php: 102 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -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. Loading @@ -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; Loading templates/submitted.html.twig +2 −3 Changes for templates/submitted.html.twig: 2 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -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. Loading @@ -34,7 +33,7 @@ */ #} <footer> {{ author_picture }} {{ user_picture }} <div{{ author_attributes }}> {% if link_path -%} {% if date -%} Loading Loading
manage_display.module +11 −12 Changes for manage_display.module: 11 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -107,6 +107,7 @@ function manage_display_theme() { 'base hook' => 'username', 'variables' => [ 'date' => NULL, 'user_picture' => NULL, 'metadata' => [], 'account' => NULL, 'attributes' => [], Loading @@ -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']); } Loading @@ -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; }
src/Plugin/Field/FieldFormatter/SubmittedFormatter.php +102 −3 Changes for src/Plugin/Field/FieldFormatter/SubmittedFormatter.php: 102 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -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. Loading @@ -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; Loading
templates/submitted.html.twig +2 −3 Changes for templates/submitted.html.twig: 2 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -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. Loading @@ -34,7 +33,7 @@ */ #} <footer> {{ author_picture }} {{ user_picture }} <div{{ author_attributes }}> {% if link_path -%} {% if date -%} Loading