As discussed here there should be a way to allow contrib and custom modules to hook into this module so that they can implement automatic labels.

Comments

Peacog created an issue. See original summary.

peacog’s picture

Category: Feature request » Task
smith76’s picture

Hello,
The last patch auto_entity_label-2755445-20.patch works perfectly with "entity_browser" but I encounter the same error with the module "Profile": Symfony\Component\Routing\Exception\RouteNotFoundException: Route "entity.profile_type.auto_label" does not exist. in Drupal\Core\Routing\RouteProvider->getRouteByName() (line 187 of core\lib\Drupal\Core\Routing\RouteProvider.php)
Thank you for your help

peacog’s picture

Hi @Smith76, please try the latest patch #22 over on the main issue: #2755445: Auto Entity label does not set entity labels and breaks contrib modules. I think it will fix the error you're seeing.

smith76’s picture

Great ... it works fine :-)

smith76’s picture

Hi,
Sorry but new bug with the experimental module of core drupal "Content Moderation" :
Recoverable fatal error: Argument 1 passed to Drupal\auto_entitylabel\EntityDecorator::decorate() must be an instance of Drupal\Core\Entity\ContentEntityInterface, instance of Drupal\node\Entity\NodeType given, called in PATH\modules\auto_entitylabel\auto_entitylabel.module on line 52 and defined in Drupal\auto_entitylabel\EntityDecorator->decorate() (line 74 of modules\auto_entitylabel\src\EntityDecorator.php).

peacog’s picture

Thanks @Smith76, I'll take a look. Would you mind posting your comments over on the other issue please #2755445: Auto Entity label does not set entity labels and breaks contrib modules? This issue is for a piece of functionality that hasn't been written yet. Thanks.

brunodbo’s picture

I'm looking to use auto_entitylabel with ECK (https://www.drupal.org/project/eck) in D8.

- Would integration between auto_entitylabel and ECK happen through the hook proposed in this issue?
- Is it possible to briefly describe what such a hook in auto_entitylabel would look like (what it should do, how it should work)? Looking for some pointers to help out with this. Thanks!

brunodbo’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
darrick’s picture

I don't think a hook is required. I had success setting the ['links']['auto-label'] in the ConfigEntityType annotation.
Automatic Entity Label sets the above via hook_entity_type_alter() by ....
$entity_type->setLinkTemplate('auto-label', $entity_type->getLinkTemplate('edit-form') . "/auto-label");

hypertext200’s picture

hypertext200’s picture

Following code does not work for Custom ECK entities. Any other suggestions?

$entity_type->setLinkTemplate('auto-label', $entity_type->getLinkTemplate('edit-form') . "/auto-label");

daggerhart’s picture

Took me a little bit of playing around, but I finally got this working generically with ECK.

Here is an example hook that will enable the auto-label form for all ECK bundles:

/**
 * Implements hook_entity_type_alter().
 */
function EXAMPLE_entity_type_alter(array &$entity_types)
{
  foreach ($entity_types as $entity_type) {
    if ($entity_type->getProvider() == 'eck' && !is_null($entity_type->getBundleOf())){
      $entity_type->setLinkTemplate('auto-label', $entity_type->getLinkTemplate('edit-form') . "/auto-label");
    }
  }
}

Note: that the "Automatic Label" link does not appear as a tab when editing a bundle, but it is available in the dropdown menu for a bundle when viewing the bundle list of an ECK entity type.

Additionally I wanted easy access to the tokens available to the entity, so here is an example hook of adding the relevant tokens to the token browser:


/**
 * Implements hook_form_FORM_ID_alter().
 */
function EXAMPLE_form_auto_entitylabel_settings_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $path = \Drupal::request()->getpathInfo();
  $arg  = explode('/',$path);
  if ($arg[2] == 'eck') {
    $form['auto_entitylabel']['token_help']['#token_types'][] = $arg[5];
  }
}

Ideally, these things are included in this module in some (likely-better) way.

robpowell’s picture

@daggerhart Thanks so much for posting example. This allowed me to extend this module for group. I am working off of dev and wanted to add that the tabs and tokens did show up.

brooke_heaton’s picture

Would it be desirable to add the form_alter functionality to this module for Groups? The custom solution isn't very accessible to many site builders?

brooke_heaton’s picture

This is working for me for the Groups module

/**
 * Implements hook_entity_type_alter().
 */
function group_entity_type_alter(array &$entity_types)
{
  foreach ($entity_types as $entity_type) {
    if ($entity_type->getProvider() == 'group' && !is_null($entity_type->getBundleOf())){
      $entity_type->setLinkTemplate('auto-label', $entity_type->getLinkTemplate('edit-form') . "/auto-label");
    }
  }
}
kclarkson’s picture

@brooke_heaton,

I just ran into this issue today.

It would be super awesome for someone to provide a patch to allow site builders to use this with ECK :)

thanks again

bgilhome’s picture

Another way to add tokens for your own custom entity type MYTYPE (defined in MYMODULE):

function MYMODULE_form_auto_entitylabel_settings_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  // Provide entity tokens to the auto_entitylabel settings form.
  if (\Drupal::routeMatch()->getRawParameters()->has('MYTYPE')) {
    $form['auto_entitylabel']['token_help']['#token_types'][] = 'MYTYPE';
  }
}

(Presuming your entity type edit route is defined with a path including a '/{MYTYPE}/' parameter).

anybody’s picture

Component: Code » Documentation

Thank you very much. I think this should be documented in an .api.php file for other developers shouldn't it?
That should list up the several ways mentioned here to add support to custom entity types.

Thank you all very much so far!

daggerhart’s picture

Status: Active » Needs review
StatusFileSize
new1.59 KB

Here's a first-pass at adding these examples to an .api.php file within the module.

Anonymous’s picture

Status: Needs review » Needs work

Tried the approach in #13 with eck and the provided patch installed.

However, this doesn't seem to actually work. While it adds the auto entity label form to ECK, it doesn't actually perform any of the actions on the eck entity when saved.

For example is I select "Automatically generate the label and hide the label field" and go edit the eck entity, the title field is still there. It also doesn't auto generate the label.

daggerhart’s picture

Status: Needs work » Needs review
Related issues: +#2923876: Broken when provider !=entityTypeId

Hmmm... I was 99% sure this worked, so I dug into the code a bit and found a discrepancy in how the config data is stored for entity bundles that leverage this module.

On saving the settings form, it saves the values to a config object using the entity type provider id as a key. But in the entity decorator, it is looking for the settings using the entity type as a key.

This issue has a working patch that addresses the problem #2923876: Broken when provider !=entityTypeId. With that patch applied this example code works for me. The only problem with the patch as I see it is that it doesn't provide an update hook that automatically fixes the stored config keys. I will comment on that issue with the suggestion.

brooke_heaton’s picture

Huh, bummer. #13 is working for me on my Group type per #16.

scottrigby’s picture

Agree with #22 this works well with #2923876: Broken when provider !=entityTypeId

caldenjacobs’s picture

StatusFileSize
new1.72 KB

Re-roll of #20 for latest dev release.

As with #22, works with #2923876: Broken when provider !=entityTypeId

kris77’s picture

@caldenjacob,
Do I have to replace "MYMODULE" with "auto_entitylabel"?

Thanks.

zanvidmar’s picture

Fist thank you for all work and patches made for this module.

I took me some time to gather all data from this post and from https://www.drupal.org/project/auto_entitylabel/issues/2923876 to make it work.
I am posting this for anyone that want to enable automatic label for their custom entities.

1. use development version
composer require 'drupal/auto_entitylabel:2.x-dev'

2. Apply two patches:
- https://www.drupal.org/files/issues/2018-09-22/2829571-provide-a-hook-fo...
- https://www.drupal.org/files/issues/2019-01-19/auto_entitylabel-2923876-...

3. Create custom hook

Where "HOOK" is "your_module_name" (@Kris77)
and "your_module" must be replaced with module that provides your custom entity

/**
 * Implements hook_entity_type_alter().
 */
function HOOK_entity_type_alter(array &$entity_types) {
  foreach ($entity_types as $entity_type) {
    if ($entity_type->getProvider() === 'your_module' && !is_null($entity_type->getBundleOf())){
      $entity_type->setLinkTemplate('auto-label', $entity_type->getLinkTemplate('edit-form') . "/auto-label");
    }
  }
}

Based on my testing with approach described above, both patches on dev branch works perfectly.

skdrupal88’s picture

Thanks for patches, and thanks @zanvidmar, your instruction works great.

willabby’s picture

@zanvidmar thanks for your instructions. I am trying to get this to work with Group module (8.x-1.0-rc2) and Auto Entity Label dev version. Following your instructions, Auto Entity Label works on the Group Type, but does not work on the Group Content Type (i replaced 'group' with 'group_content' for entity type. It only adds the "Auto Label" tab with a path to /auto-label, which results in error message. Do you have otherwise?

Auto Entity Label is also not working on any of my content types, I get the following error message saving a node with AEL:

`Cannot load GroupContent entities for an unsaved entity`

I would be interested to know if you have getting any similar errors with Group Module. Thank you.

zanvidmar’s picture

@willabby where did you replaced "'group' with 'group_content'" ? Can you share the code?
I did not tried this with Group module (yet).

willabby’s picture

@zanvidmar this is the code I put in a custom module, I wasn't sure if where you had 'your_module' if I should put the module providing the entity type (gnode)? or the actual entity type (group_content), but I tried both, and both resulted in same error. (As I mentioned though, the first `foreach` for `group` works on the group entity per your instructions)

/**
 * Implements hook_entity_type_alter().
 */
function mymodule_entity_type_alter(array &$entity_types)
{
  foreach ($entity_types as $entity_type) {
    if ($entity_type->getProvider() == 'group' && !is_null($entity_type->getBundleOf())){
      $entity_type->setLinkTemplate('auto-label', $entity_type->getLinkTemplate('edit-form') . "/auto-label");
    }
  }
  foreach ($entity_types as $entity_type) {
    if ($entity_type->getProvider() == 'gnode' && !is_null($entity_type->getBundleOf())){
      $entity_type->setLinkTemplate('auto-label', $entity_type->getLinkTemplate('edit-form') . "/auto-label");
    }
  }
}

Also, if I click on the automatic label tab for the group content configuration (I was not sure if I set autolabel on the content type, if I needed to do anything with the group content entity too?), i get this error:

Error: Call to a member function id() on null in Drupal\auto_entitylabel\Form\AutoEntityLabelForm->__construct() (line 103 of modules/contrib/auto_entitylabel/src/Form/AutoEntityLabelForm.php).

Thank for any help you can provide!

UPDATE: My issue seemed to be caused by this patch (https://www.drupal.org/project/group/issues/2774827) providing tokens for group module, when I updated patch to latest version, error went away, and I am able to use auto entity label on nodes now.

colan’s picture

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

I'm not sure how to make this work for custom entities. I have:

  foreach ($entity_types as $entity_type) {
    if ($entity_type->getBundleOf() == 'time_slot'){
      $entity_type->setLinkTemplate('auto-label', '/admin/structure/time_slot/auto-label');
    }
  }

But that route is showing page not found.

Why would it be $entity_type->getLinkTemplate('edit-form') . "/auto-label"? It seems like this would be the url for editing each entity, not the overall entity settings?

BTW the patch in https://www.drupal.org/project/auto_entitylabel/issues/2923876 is applied to 2.x and 3.x so not needed anymore.

mortona2k’s picture

liquidcms’s picture

Wondering if there is a solution today for supporting AEL on contrib entities?

I have a bundless entity called "sitewide alert" (https://www.drupal.org/project/sitewide_alert) with core 9.1 and latest AEL and:
- patch from here
- this code:

function mycustom_entity_type_alter(array &$entity_types) {
  foreach ($entity_types as $entity_type) {
    if ($entity_type->getProvider() == 'sitewide_alerts') { //} && !is_null($entity_type->getBundleOf())){
      $entity_type->setLinkTemplate('auto-label', $entity_type->getLinkTemplate('edit-form') . "/auto-label");
    }
  }
}

and when i go to the entity config page for the entity type: admin/config/sitewide_alerts and add on auto-label, i get 404.

I see there is a very old patch for bundleless which does not apply to current release. Is a refactored version of that patch the missing piece here?

dqd’s picture

Status: Needs review » Needs work

Would love to see somebody chiming in here for a reroll considering all the latest comments. I will review asap and commit if RTBC.

pasqualle’s picture

Status: Needs work » Reviewed & tested by the community

The patch still applies, and it only adds information to the API documentation. I have nothing to improve for this patch.

vladimiraus’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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