Problem/Motivation

Some entities, like core's User entity, don't have bundles; or rather, they have exactly one bundle with the same name as the entity, but no type configuration entity (like NodeType for Node entities). See the Entity topic on api.drupal.org for more information.

Currently, however, auto_entitylabel is only capable of generating automatic entity labels for entities that have type configuration entities. In particular, \Drupal\auto_entitylabel\Form\AutoEntityLabelForm exits with a fatal error (aka WSOD) if an entity type configuration entity cannot be found. My gut (which could be wrong!) says AutoEntityLabelForm is probably the biggest hurdle in this.

However, given that this module simply constructs a label string from tokens, I don't think it's necessary to require type configuration entities.

Related issues

From what I can tell, the following issues are related to, and may be partially or fully fixed by completing this plan/issue:

Proposed resolution

Modify this module so that it will function without requiring type configuration entities.

Remaining tasks

  1. #2923876: Broken when provider !=entityTypeId
  2. #2945361: Clean up AutoEntityLabelForm (postponed in favor of 2923876)

User interface changes

Unknown; but hopefully none.

API changes

Unknown, but hopefully the public API for this module will not change.

Data model changes

Unknown, but hopefully none.

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

mparker17 created an issue. See original summary.

mparker17’s picture

pancho’s picture

Status: Active » Needs review
StatusFileSize
new13.09 KB

I think I got it working... :)

pancho’s picture

StatusFileSize
new15.92 KB

Sorry, wrong patch in #3.

pancho’s picture

StatusFileSize
new16.07 KB

Now this definitely is the right patch. Manually tested to work as advertised.

I got Automatic Entity Labels working for:
* custom bundle-less content entities implementing the link template 'auto-label'
* Core's bundle-less, fieldable content entities (= Feed, User).

The enclosed, comprehensive patch against latest dev builds upon and therefore includes the other patch in #2945361-5: Clean up AutoEntityLabelForm. Would be great to start with having #2923876-28: Broken when provider !=entityTypeId committed, so we can get forward with this.

w01f’s picture

This patch lets auto entity label work great with users - is it possible to extend this to work with commerce store setup at: https://www.supersite.com/admin/commerce/config/store-types/online/edit/...

Update: Also would be great if it worked with commerce products: https://www.supersite.com/admin/commerce/config/product-types/awesome-pr...

colan’s picture

Version: 8.x-2.x-dev » 8.x-3.x-dev
dww’s picture

Status: Needs review » Needs work

Alas, #5 no longer applies cleanly to either 8.x-2.x nor 8.x-3.x branches. Reroll would be wonderful. @Pancho is that easy for you?

Thanks!
-Derek

dww’s picture

Meanwhile, some minor nits I found reviewing #5 closely:

  1. +++ b/auto_entitylabel.module
    @@ -55,8 +56,23 @@ function auto_entitylabel_entity_type_alter(array &$entity_types) {
    +            break;
    +          case 'user':
    

    Code style to have newlines after break;

  2. +++ b/auto_entitylabel.module
    @@ -96,30 +112,37 @@ function auto_entitylabel_prepare_entityform(&$form, ContentEntityInterface $ent
    +    } else {
    

    CS:

    }
    else {
    
  3. +++ b/src/Form/AutoEntityLabelForm.php
    @@ -157,11 +137,8 @@ class AutoEntityLabelForm extends ConfigFormBase {
    -    /*
    -     * @todo
    -     *  Find a generic way of determining if the label is rendered on the
    -     *  entity form. If not, don't show 'auto_entitylabel_optional' option.
    -     */
    +    // @TODO: Find a generic way of determining if the label is rendered on the
    +    // entity form. If not, don't show 'auto_entitylabel_optional' option.
    

    Out of scope.

  4. +++ b/src/Plugin/Derivative/AutoEntityLabelConfigTask.php
    @@ -47,12 +48,12 @@ class AutoEntityLabelConfigTask extends DeriverBase implements ContainerDeriverI
    -      // Special handling of Taxonomy. See https://www.drupal.org/node/2822546
    -      if ($entity_type_id == "taxonomy_vocabulary") {
    -        $base_route = "entity.{$entity_type_id}.overview_form";
    +      if ($entity_type instanceof ContentEntityType) {
    +        $base_route = $entity_type->get("field_ui_base_route");
           }
           else {
    -        $base_route = "entity.{$entity_type_id}.edit_form";
    +        // Special handling of Taxonomy. See https://www.drupal.org/node/2822546
    +        $base_route = $entity_type_id == "taxonomy_vocabulary" ? "entity.taxonomy_vocabulary.overview_form" : "entity.{$entity_type_id}.edit_form";
           }
    

    I think this would be more readable as:

    if ($entity_type instanceof ContentEntityType) {
      $base_route = $entity_type->get("field_ui_base_route");
    }
    // Special handling of Taxonomy. @see https://www.drupal.org/node/2822546
    elseif ($entity_type_id == "taxonomy_vocabulary") {
      $base_route = 'entity.taxonomy_vocabulary.overview_form';
    }
    else {
      $base_route = "entity.{$entity_type_id}.edit_form";
    }
    

Excited to test a working patch... seems like a big win for auto_entitylabel!

Thanks,
-Derek

bdimaggio’s picture

Re-rolled against current 8.x-3.x. This is working for a bundle-less custom entity we're using, although I should note that I haven't tested with bundle-less core entities like User.

Also note that in order to take advantage of this new state of the code, I had to add a hook in my own custom module:

/**
 * Implements hook_entity_type_alter().
 */
function mymodule_entity_type_alter(array &$entity_types) {
  foreach ($entity_types as $entity_type) {
    if ($entity_type->getProvider() == 'mymodule' && $entity_type->getLabel() == 'MyCustomEntityType') {
      $path = '/admin/structure/mycustomentitytype/settings';
      $entity_type->setLinkTemplate('auto-label', $path . '/auto-label');
    }
  }
}
dkosbob’s picture

Thanks for this, y'all. This is working well when updating an existing user, but it's preventing a new user from being created due to the percent signs in the widget default value ('%AutoEntityLabel%').

I just made a very small change to use '.AutoEntityLabel.' as the default value instead. It looks arbitrary to me, but I may be missing something

colan’s picture

Let's start providing interdiffs with patches so they're easier to review. See https://www.drupal.org/documentation/git/interdiff .

dkosbob’s picture

StatusFileSize
new0 bytes

That's empty, pulled the trigger too early, sorry.

pasqualle’s picture

Title: [Plan] Support entities without bundles (User, etc.) » Support entities without bundles (User, etc.)
Category: Plan » Feature request
Status: Needs work » Needs review
StatusFileSize
new11.78 KB

reroll

tijsdeboeck’s picture

Tested patch #14 with 3.0.0-beta4, works for me!

daniel-san’s picture

Hoping to help, but I have been trying to apply patch #14 to dev version with no success. I keep getting errors.

I downloaded the patch into the dev version of the module and then used git apply -v with the patch name.

errors:

Checking patch auto_entitylabel.module...
error: while searching for:
*/

use Drupal\auto_entitylabel\AutoEntityLabelManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;

error: patch failed: auto_entitylabel.module:6
error: auto_entitylabel.module: patch does not apply
Checking patch src/AutoEntityLabelManager.php...
Checking patch src/Form/AutoEntityLabelForm.php...
error: while searching for:

use Drupal\auto_entitylabel\AutoEntityLabelManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

error: patch failed: src/Form/AutoEntityLabelForm.php:4
error: src/Form/AutoEntityLabelForm.php: patch does not apply
Checking patch src/Plugin/Derivative/AutoEntityLabelConfigTask.php...

Maybe I'm doing it wrong? I've tried to follow the applying patches guidelines and all, but I'm not having any success. If anyone wants to provide a bit of guidance, I'd be glad to put some time into testing.

Thanks.
Dan

pasqualle’s picture

Status: Needs review » Needs work

Yes, it seems the patch needs a new reroll. It does not apply to the latest dev release any more.

daniel-san’s picture

Ok, thank you so much for the response. Is there something I can do, or do we just wait to test after the re-roll?

pasqualle’s picture

Status: Needs work » Needs review
StatusFileSize
new7.68 KB

reroll, with unnecessary changes removed.

daniel-san’s picture

Drupal 9.3.13
auto_entitylabel 8.x-3.x-dev
Patch #19 applied

Created First Name field on set Auto Label to 'Automatically generate the label and hide the label field' with token [user:field_first_name]

Created new user with a first name in field and it set the user title as expected. Changed the first name field to another name and it updated correctly.

Tried creating a custom module per comment #10 for a custom entity in my dev site and could not get a auto label page for that entity.

Is this code supposed to allow for custom entities to have auto label at this point, or is this just for the User entity?

pasqualle’s picture

For custom entities the patch from issue #2829571: Provide a hook for contrib modules that want to implement automatic labels is needed, and also need to implement the new hook explained in the issue.

daniel-san’s picture

From your strikethrough on comment #21, I take it that you mean to apply the patch in #19 and then use the following hook customized for the custom entity.

I have auto_entitylabel 8.x-3.x-dev installed.
I have applied the patch from #19.
I have created a custom module to implement the hook for the override. I my case, the custom module is referencing my entity module called 'student_entity' and the entity is 'student'
After installing, patching, and then writing and installing the custom module for the hook, I get a 'Page not found' when going to the path and adding /auto-label to the end.

my implementation of the hook:

function autoentitylabel_override_entity_type_alter(array &$entity_types) {
  foreach ($entity_types as $entity_type) {
    if ($entity_type->getProvider() == 'student_entity' && $entity_type->getLabel() == 'student') {
      $path = '/admin/structure/student';
      $entity_type->setLinkTemplate('auto-label', $path . '/auto-label');
    }
  }
}

In the path, I used '/admin/structure/student' because that's how you get to the entity settings page. Adding settings on the end, as in comment #10, gets a page not found as well. So, I am adjusting the path in my hook.

Hoping I'm on the right track and can provide some positive feedback.

peter caritas’s picture

This is what worked for me:

Apply the patch in #19

In the .module file for my custom entity module I added the following:

/**
 * Implements hook_entity_type_alter().
 */
function mymodule_entity_type_alter(array &$entity_types) {

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types['my_entity']->setLinkTemplate('auto-label', '/admin/structure/my_entity/settings/auto-label');

}

Replacing mymodule with the name of your module and my_entity with the name of the entity (and any other changes to the path to match where your entity admin lives.

adaddinsane’s picture

I just added this and this worked for me:

function mymodule_entity_type_alter(array &$entity_types): void {
  /** @var EntityTypeInterface[] $entity_types */
  $entity_types['my_entity']->setLinkTemplate('auto-label', '/admin/structure/my_entity/auto-label');
}

Including "/settings" did not work.

Though if you are a developer and using custom entities then you should have access to the entity configuration so you just need to add this:

 *   links = {
 *     "canonical" = "/admin/structure/my_entity/{my_entity}",
 *     "add-form" = "/admin/structure/my_entity/add",
 *     "edit-form" = "/admin/structure/my_entity/{my_entity}/edit",
 *     "delete-form" = "/admin/structure/my_entity/{my_entity}/delete",
 *     "collection" = "/admin/structure/my_entity",
 *     "auto-label" = "/admin/structure/my_entity/auto-label",
 *   },

which is a lot cleaner.

adriancid’s picture

For custom entities #19 and #23 made the job.

jonmcl’s picture

Status: Needs review » Needs work

Potentially major problem with the else statement in this block of code:

    // Special treatment for Core's user entity.
    if ($entity_type_id == 'user') {
      $widget = &$form['account']['name'];
    }
    else {
      $widget = &$form[$label]['widget'][0]['value'];
    }

Not all entity form displays are going to have a label field in the form. Assigning a non-existent form element to $widget by reference actually created the element with a value of null;

Problem is that ContentEntityForm::copyFormValuesToEntity then gets called and because the label widget was added to the form (with a value of null), ::copyFormValuesToEntity then sets the label to be null.

This is probably a problem only when auto_entitylabel is disabled for a particular entity type and when the label widget is not present in the form currently being submitted.

UPDATE: I think this is a bug in the module itself and not directly related to this patch.

FINAL UPDATE: Never mind. I am an idiot :)
It appears we had a form item element (markup) using 'label' as it's key and that is why ::copyFormValuesToEntity was attempting to set the entity's label.

jonmcl’s picture

Status: Needs work » Needs review

Setting back to "Needs review"

joegraduate’s picture

StatusFileSize
new7.69 KB

Re-rolled patch from #19 against latest 8.x-3.x branch. The existing patch no longer applied after the latest changes included in the 8.x-3.2 release.

orkutmuratyilmaz’s picture

Status: Needs review » Reviewed & tested by the community

the patch #28 works great:)

liam morland made their first commit to this issue’s fork.