Problem/Motivation

Theme hook "file_widget" lives in core but is not used. File widget uses "file_managed_file" instead.

Proposed resolution

Remove unused theme hook.

Original issue summary

Part of #2025629: [PP-1] [meta] Ensure that all #theme/#theme_wrappers hooks provided by core are associated with a sensible, re-usable element #type. Not sure if I prefer 'file_managed_file' or 'managed_file' but the two should share the same name as they're just two parts of the same thing.

Comments

joelpittet’s picture

Issue summary: View changes

Wondering if 'widget' should be in the name, because 'file_widget' extends from #type 'managed_file'

thedavidmeister’s picture

What about 'managed_file' for the #type/#theme and 'managed_file_widget' for the widget extending the base #type?

joelpittet’s picture

That makes sense to me! I wonder if we can still get this kind of change in for D8 beta?

slashrsm’s picture

Not sure if this is related or not...

I was looking into this while trying to solve another issue and realized that file_widget theme isn't even used any more. It seems that file_managed_file gets picked every time instead of it.

As part of #2511036: image-widget.html.twig never gets used (image_widget gets overridden by FieldWidget::process) this hunk went in:

diff --git a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
index acba472..5852ab4 100644
--- a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
+++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
@@ -370,7 +370,10 @@ public static function process($element, FormStateInterface $form_state, $form)
     $item = $element['#value'];
     $item['fids'] = $element['fids']['#value'];
 
-    $element['#theme'] = 'file_widget';
+    // Prevent the file widget from overriding the image widget.
+    if (!isset($element['#theme'])) {
+      $element['#theme'] = 'file_widget';
+    }

Intent of this hunk was to prevent file widget from overriding theme that image widget sets. Since file managed element sets theme to file_managed_file this condition always stays unsatisfied and file_widget theme unused as a result of that.

I am wondering if we need file_widget at all?

star-szr’s picture

@slashrsm we probably don't need it in core but I'm thinking the fact that it's there and it's RC means we can't remove it.

slashrsm’s picture

Title: #theme 'file_managed_file' and #type 'managed_file' should have the same name. » Remove unused #theme "file_widget"
Version: 8.0.x-dev » 9.x-dev
Issue summary: View changes

@Cottser: I had the same thought too. This probably means D9 material?

slashrsm’s picture

Issue tags: +Novice
catch’s picture

Version: 9.x-dev » 8.0.x-dev
Issue tags: +rc target triage

This is very confusing and I think we should consider it as borderline 'dead code removal' for an RC target.

If a module is relying on this, I'd be very surprised.

If not, we should at least deprecate it somehow.

joelpittet’s picture

Status: Active » Needs review
StatusFileSize
new5.58 KB

Removed, let's see what happens:)

andypost’s picture

Looks great, except following

+++ b/core/lib/Drupal/Core/Template/Attribute.php
@@ -117,10 +117,11 @@ public function offsetSet($name, $value) {
-    // If the value is already an AttributeValueBase object, return it
-    // straight away.
+    // If the value is already an AttributeValueBase object,
+    // return a new instance of the same class, but with the new name.
     if ($value instanceof AttributeValueBase) {
-      return $value;
+      $class = get_class($value);
+      return new $class($name, $value->value());

how this change related?

star-szr’s picture

Status: Needs review » Needs work

Yeah I think there's some definitely some unrelated changes in there.

joelpittet’s picture

Status: Needs work » Needs review
StatusFileSize
new3.65 KB

Whoops, thanks for the review. Well all works;)

star-szr’s picture

Looks good my only thought is there's maybe some code that would be dead in core/modules/file/file.js as a result:

  Drupal.behaviors.filePreviewLinks = {
    attach: function (context) {
      $(context).find('div.js-form-managed-file .file a, .file-widget .file a').on('click', Drupal.file.openInNewWindow);
    },
    detach: function (context) {
      $(context).find('div.js-form-managed-file .file a, .file-widget .file a').off('click', Drupal.file.openInNewWindow);
    }
  };

Found that grepping for file_widget and file-widget.

slashrsm’s picture

Status: Needs review » Reviewed & tested by the community

That JS is not dead. It is still used by file/image widget. Standard install, node/add/article, upload an image in image field, image link that appears is handeled by that code.

In JS console on the same page:

> jQuery('div.js-form-managed-file .file a, .file-widget .file a');

[<a href=​"http:​/​/​localhost/​drupal8/​sites/​default/​files/​2015-11/​18129594179_a842d51637_k.jpg" type=​"image/​jpeg;​ length=2018497" target=​"_blank">​18129594179_a842d51637_k.jpg​</a>​]
star-szr’s picture

I'm not saying all those lines of JS are dead but the .file-widget .file a selector sure seems like it would be :)

joelpittet’s picture

StatusFileSize
new772 bytes
new4.4 KB

I agree, not changing status because I believe that is all the things.

effulgentsia’s picture

Assigned: Unassigned » catch

I discussed this with @xjm to triage, but @catch wasn't online. Assigning to him, so that we figure it out when he is.

effulgentsia’s picture

Assigned: catch » Unassigned
Issue tags: -rc target triage +rc target

Ah, comment #4 explains why/how file-widget.html.twig is currently dead code. Yeah, this is better to clear up pre-release than have to support until 9.x.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.0.x, thanks!

  • catch committed 5bd9271 on
    Issue #2025707 by joelpittet: Remove unused #theme "file_widget"
    
nod_’s picture

Issue tags: +JavaScript

  • catch committed 5bd9271 on 8.1.x
    Issue #2025707 by joelpittet: Remove unused #theme "file_widget"
    

Status: Fixed » Closed (fixed)

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