Problem/Motivation

The template suggestions have a "custom" view mode. It would be nice to have the correct view mode in the template suggestions.

Steps to reproduce

<?php

namespace Drupal\example\Plugin\ExtraField\Display;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\extra_field\Plugin\ExtraFieldDisplayFormattedBase;

/**
 * Extrafield formatter.
 *
 * @ExtraFieldDisplay(
 *   id = "node_type",
 *   label = @Translation("Node type"),
 *   bundles = {
 *     "node.*"
 *   }
 * )
 */
class NodeType extends ExtraFieldDisplayFormattedBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(ContentEntityInterface $entity) {
    return [
      '#markup' => $entity->type->entity->label(),
    ];
  }

}

Suggestions:

<!-- FILE NAME SUGGESTIONS:
   * field--node--extra-field-node-type--article---custom.html.twig
   * field--node--extra-field-node-type---custom.html.twig
   * field--node--extra-field-node-type--article.html.twig
   * field--node--extra-field-node-type.html.twig
   * field--node--article.html.twig
   * field--extra-field-node-type.html.twig
   * field--extra-field.html.twig
   x field.html.twig
-->

Suggestions should be, with a "alert" view mode:

<!-- FILE NAME SUGGESTIONS:
   * field--node--extra-field-node-type--article---alert.html.twig
   * field--node--extra-field-node-type---alert.html.twig
   * field--node--extra-field-node-type--article.html.twig
   * field--node--extra-field-node-type.html.twig
   * field--node--article.html.twig
   * field--extra-field-node-type.html.twig
   * field--extra-field.html.twig
   x field.html.twig
-->
CommentFileSizeAuthor
#6 3202973-6.patch797 bytestimohuisman
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

GuillaumeDuveau created an issue. See original summary.

jnettik’s picture

This seems to be done deliberately to get around the Quickedit module. In ExtraFieldDisplayFormattedBase::view() there's this array:


      $build = [
        '#theme' => 'field',
        '#title' => $this->getLabel(),
        '#label_display' => $this->getLabelDisplay(),
        // Prevent quickedit from editing this field by using a special view
        // mode.
        // @see quickedit_preprocess_field()
        '#view_mode' => '_custom',
        '#language' => $this->getLangcode(),
        '#field_name' => $this->getFieldName(),
        '#field_type' => $this->getFieldType(),
        '#field_translatable' => $this->isTranslatable(),
        '#entity_type' => $entity->getEntityTypeId(),
        '#bundle' => $entity->bundle(),
        '#object' => $entity,
        '#formatter' => $this->getPluginId(),
      ];

I got around this issue (I'm not using Quickedit) by adding a ::view() method to my class and overrode the view mode:


  /**
   * {@inheritdoc}
   */
  public function viewElements(ContentEntityInterface $entity) {
    // Do stuff and return my elements array.
  }

  /**
   * {@inheritdoc}
   */
  public function view(ContentEntityInterface $entity) {
    $build = parent::view($entity);
    $build['#view_mode'] = $this->getViewMode();

    return $build;
  }

prudloff made their first commit to this issue’s fork.

prudloff’s picture

Status: Active » Needs review

The MR keeps the quickedit workaround but also adds template suggestions back.
It also keeps the -custom suggestion for retro-compatibility.

timohuisman’s picture

StatusFileSize
new797 bytes

I'm not sure why, but I get a 404 when I try to create a new branch for this issue.

Anyhow, Quickedit is no longer in core, see #3259831: Quick Edit is deprecated. So we could just use the existing view mode instead of overwriting it.

Status: Needs review » Needs work

The last submitted patch, 6: 3202973-6.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

prudloff’s picture

Status: Needs work » Needs review

I agree that dropping the quickedit workaround would be the easiest way to fix this.

pcambra’s picture

Version: 8.x-2.x-dev » 3.0.x-dev
arousseau’s picture

Status: Needs review » Reviewed & tested by the community

MR 13 applies cleanly to 3.0 and I get the following template suggestions, with the correct view mode.
It looks safe to merge.

 FILE NAME SUGGESTIONS:
   ▪️ field--node--extra-field-formatted-field--page--full.html.twig
   ▪️ field--node--extra-field-formatted-field--full.html.twig
   ▪️ field--node--extra-field-formatted-field--page.html.twig
   ▪️ field--node--extra-field-formatted-field.html.twig
   ▪️ field--node--page.html.twig
   ▪️ field--extra-field-formatted-field.html.twig
   ▪️ field--extra-field.html.twig
   ✅ field.html.twig

  • pcambra committed 504b4772 on 3.0.x authored by prudloff
    Issue #3202973: Wrong "custom" view mode in template suggestions
    
pcambra’s picture

Status: Reviewed & tested by the community » Fixed

Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

prudloff’s picture

Is there a plan to publish a release containing this fix?