Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DamienMcKenna’s picture

Some wip JS code to chew on:

(function($) {
  // Provide a 'remove' button on entityreference fields.
  Drupal.behaviors.beans_ref_revisions = {
    attach: function (context, settings) {
      $('.field-type-entityreference tr').each(function(i) {
        $('.references-dialog-links', this).each(function(i) {
          if ($('li.first a.edit-dialog', this).length > 0) {
            $('li.last', this).append('<a href="#">Remove</a>').addClass('remove');
            $('li.remove a', this).click(function() {
              // Clear the autocomplete field.
              var $td = $(this).parents('td');
              $('.form-type-textfield input', $td).val('');

              // Remove both the 'edit' and 'remove' links.
              var $ul = $(this).parents('ul');
              $('li', $ul).detach();

              return false;
            });
          }
        });
      });
    }
  }
})(jQuery);

The question is how should this work:

  • Should the entire field be removed or just the field reset?
  • If the field should be reset should it not also add the "create" links?

I think that the links should be more intelligent anyway, they should recognize when the field has changed and show/hide the edit and/or create links as appropriate.

DamienMcKenna’s picture

Reading through the code more, I think the links themselves should be inserted via JS so that they can be more readily manipulated rather than only being inserted via PHP.

DamienMcKenna’s picture

Updated JS that hides the row entirely.

(function($) {
  // Provide a 'remove' button on entityreference fields.
  Drupal.behaviors.beans_ref_revisions = {
    attach: function (context, settings) {
      $('.field-type-entityreference tr').each(function(i) {
        $('.references-dialog-links', this).each(function(i) {
          if ($('li.first a.edit-dialog', this).length > 0) {
            $('li.last', this).append('<a href="#">Remove</a>').addClass('remove');
            $('li.remove a', this).click(function() {
              // Hide this row entirely.
              $(this).parents('tr').hide();

              // Clear the autocomplete field.
              var $td = $(this).parents('td');
              $('.form-type-textfield input', $td).val('');

              return false;
            });
          }
        });
      });
    }
  }
})(jQuery);
DamienMcKenna’s picture

I'm not the best at JS, so feel free to poke fun at it :)

DamienMcKenna’s picture

Related issue for EntityReference: #1946010: Improve UX around clearing an item

DamienMcKenna’s picture

DamienMcKenna’s picture

Status: Active » Needs review
FileSize
1006 bytes

A patch to provide a 'remove' link if there is an existing 'edit' link.

sk2013’s picture

Version: 7.x-1.x-dev » 7.x-1.0-alpha4

Thank you all for the good work.

We have tried the patch with the alpha4 version, but did not have any luck. Would it be possible to guide us or any pointer will help.

We are using this module with references module for adding and editing node. Edit is working fine. We want to remove the node, but we did not get the "remove" link after applying the patch.

Thanks in advance.

DamienMcKenna’s picture

@sk2013: You need to apply it to the latest -dev version.

sk2013’s picture

Thanks a lot Damien! I will update once I complete the work as suggested.

Thanks again

sk2013’s picture

Hello Damien,

I have tried with the latest -dev version and updated with this patch. But there is no "remove"link appeared. Tested in a fresh Drupal installation and there is no luck.

Is there any way we can find what's causing the "remove" link not appearing?

Thanks a Lot!

cecrs’s picture

Works beautifully, thanks Damien!

nicholas.alipaz’s picture

Issue summary: View changes

Works nicely for us.

mengi’s picture

FileSize
9.19 KB

Patch in #7 works for me with one very minor exception with the inline_entity_form module on the same page. Any interaction of the ief module will make mutiple (4-6) remove buttons appear. I attached a image to better explain. This doesn't seem to effect the functionality as the reference will still be removed if I click on any of the buttons.

liquidcms’s picture

Status: Needs review » Needs work
FileSize
2.2 KB
633 bytes

a few things wrong with this patch:

- this doesnt create the correct UL structure; it adds the new LI inside the last one as opposed to inside the UL
- doesn't incl an icon
- doesn't work for newly added items (like picking one from search)

this patch is closer:
- correct UL structure
- add icon
- fix other LI classes

but still doesn't do the .live() part of this to add remove when an item is newly added

paramnida’s picture

The patch in #15 didn't work for me. I'm using version 7.x-1.0-beta1+4-dev.

MarcoPBazz’s picture

Patch in #15 doesn't work form me.

changing the html selectors and adding few controls (because function itself create duplicate buttons with multiple value field or multiple entityreference fields) made it work

nicholas.alipaz’s picture

Status: Needs work » Needs review

updating issue status

The last submitted patch, 7: references_dialog-n1945914-7.patch, failed testing.

The last submitted patch, 15: references_dialog-1945914-15.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 17: 0001-Add-a-link-to-remove-an-item.patch, failed testing.

MarcoPBazz’s picture

Any update? Can I help?

SocialNicheGuru’s picture

Replace "Rimuovi" with a translatable string.

MarcoPBazz’s picture

Hi SocialNicheGuru,

here's the patch!

Cogax’s picture

Status: Needs work » Needs review
FileSize
1.15 KB

All previous patches selects only entity reference fields. This patch also supports an translateable link label.

Status: Needs review » Needs work

The last submitted patch, 25: 1945914-25-remove-item-link.patch, failed testing.

lklimek’s picture

Added some triggers to the code to ensure compatibility with entityreference_live_preview (see also #2682665: Automatically load entity preview when using entityreference_live_preview)

fox_01’s picture

#27 works for me with entityreference_live_preview but if you select an entity and then remove it the remove button will get hidden and does not apear again

lklimek’s picture

Version: 7.x-1.0-alpha4 » 7.x-1.x-dev
FileSize
1.08 KB

Fixed issue from comment 28 by removing lines:

 // Hide this row entirely.
  $(this).parents('li').hide();

(however, I don't understand why it was placed there by original author of the patch).

fox_01’s picture

#29 works. thanks

kmajzlik’s picture

Status: Needs work » Needs review

Just trying to schedule CI test.