Problem/Motivation

Once #2655780: Allow to configure entity types to add path base fields (so that Pathauto can apply for them) is in, we'll no longer need a custom hook_base_field_info() implementation to add the path field.

Proposed resolution

Remove it, update the test to set the necessary configuration instead.

Remaining tasks

User interface changes

API changes

Data model changes

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir created an issue. See original summary.

Berdir’s picture

Berdir’s picture

Status: Needs work » Needs review
FileSize
2.13 KB

I've merged the pathauto issue now and rebased the PR.

Note that this will not work yet on d.o testbot until pathauto has a new release. Uploading a patch as well to prove that.

Status: Needs review » Needs work

The last submitted patch, 3: 14.patch, failed testing.

The last submitted patch, 3: 14.patch, failed testing.

The last submitted patch, 3: 14.patch, failed testing.

The last submitted patch, 3: 14.patch, failed testing.

Berdir’s picture

Berdir’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: remove-2655844-8.patch, failed testing.

The last submitted patch, 8: remove-2655844-8.patch, failed testing.

rutiolma’s picture

Status: Needs work » Needs review

Uploading a new patch that applies to the latest version. This patch also adds a default configuration to enable path alias field on install/update

rutiolma’s picture

Status: Needs review » Needs work

The last submitted patch, 13: remove_file_entity_entity_base_field_info-2655844-12.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

rutiolma’s picture

Status: Needs work » Needs review
FileSize
2.77 KB

Fixing test

Berdir’s picture

Status: Needs review » Needs work
+++ b/file_entity.install
@@ -197,3 +200,22 @@ function file_entity_update_8005() {
+    $config = $config_factory->getEditable('pathauto.settings');
+    $config->set('enabled_entity_types', ['file']);

this update is wrong and assumes that no other types are enabled. needs to get the existing values and add file.

There is also no need to do this for new installations IMHO.

I'd even consider going one step further and only enabling it if there is a pathauto pattern or file aliases and otherwise just display a message that it can be enabled in pathauto if desired.

rutiolma’s picture

Indeed there's an issue with the patch.
Before fixing it I would like to understand your pov @Berdir. By default, an URL alias field is added if pathauto exits, so I just tried to keep the same behavior. Do you mean that this behavior should change?

About your "step further" I believe you're referring to the hook_update, right?

Berdir’s picture

Yes, because a setting would be more complexity. But I think the use case for requiring url aliases on files are _very_ rare, usually you do not expose that publicly and therefore don't need aliases. So I'm proposing that we enable it only if there is a good reason to. If someone does need it after all, they can easily enable it themself. So we can return a message like 'Files no longer have an alias field by default, enable it in the pathauto settings if you wish to use that.'. Then a site either sees that message when updating (it now works like any other entity type provided by contributed modules) or if necessary, we keep the field.

And yes, that code only lives in the update function, no shared function required anymore, and no changes to hook_install().

rutiolma’s picture

Status: Needs work » Needs review
FileSize
3.16 KB

Addressing @Berdir feedback

robcarr’s picture

Status: Needs review » Needs work

The patch applies although when running the database update 8201, it generates the following errors:

Entity queries must explicitly set whether the query should be access checked or not. See Drupal\Core\Entity\Query\QueryInterface::accessCheck().

robcarr’s picture

If accessCheck(TRUE) is inserted into the relevant query in the file_entity.install file, then the update runs fine:

/**
 * Update pathauto to enable path field for file entity type if there are
 * file aliases or patterns.
 */
function file_entity_update_8201() {
  if (\Drupal::moduleHandler()->moduleExists('pathauto')) {
    $file_patterns = \Drupal::entityTypeManager()->getStorage('pathauto_pattern')->loadByProperties(['type' => 'canonical_entities:file']);
    $file_aliases = \Drupal::entityTypeManager()->getStorage('path_alias')->getQuery()->condition('path', '/file/%', 'LIKE')->accessCheck(TRUE)->count()->execute();
    if (!empty($file_patterns) || $file_aliases > 0) {
      $config_factory = \Drupal::configFactory();
      $config = $config_factory->getEditable('pathauto.settings');
      $enabled_entity_types = $config->get('enabled_entity_types');
      $config->set('enabled_entity_types', array_unique(array_merge($enabled_entity_types, ['file'])));
      $config->save();
    }
    else {
      \Drupal::messenger()->addMessage(t("Files no longer have an alias field by default, enable it in the pathauto settings if you wish to use that."));
    }
  }
}

robcarr’s picture

Steven Jones’s picture

Status: Needs review » Needs work
+++ b/file_entity.install
@@ -197,3 +197,24 @@ function file_entity_update_8005() {
+    $file_patterns = \Drupal::entityTypeManager()->getStorage('pathauto_pattern')->loadByProperties(['type' => 'canonical_entities:file']);
+    $file_aliases = \Drupal::entityTypeManager()->getStorage('path_alias')->getQuery()->condition('path', '/file/%', 'LIKE')->accessCheck(TRUE)->count()->execute();

You'd actually want ->accessCheck(FALSE)

Because you don't want the access checking enabled for this query.

bharath-kondeti made their first commit to this issue’s fork.

bharath-kondeti’s picture

Updated the patch addressing #23. Please review

bharath-kondeti’s picture

Status: Needs work » Needs review
joseph.olstad’s picture

Review so far:
I grepped all Drupal 8 projects using grep . xnddx . ru , currently only the file_entity is using this api function name .

It's likely a safe change to make above, thanks for your work.

@berdir, are you ok with this change?

joseph.olstad’s picture

Assigned: Unassigned » Berdir
c_archer’s picture

Status: Needs review » Reviewed & tested by the community

Tested patch on #25 and it worked as hoped.

joseph.olstad’s picture

Gitlab didn't use the commit message I specified, grumble.

Should have been this:
git commit -m 'Issue #2655844 by rutiolma, bharath-kondeti, Berdir, robcarr, joseph.olstad, Steven Jones, c_archer: Remove file_entity_entity_base_field_info()' --author="Sascha Grossenbacher <5019-berdir@users.noreply.drupalcode.org>"

joseph.olstad’s picture

Version: 8.x-2.x-dev » 8.x-2.0-rc2
Status: Reviewed & tested by the community » Fixed

Included in rc2

webdrips’s picture

Status: Fixed » Needs work

I'm not sure if it's best to start a new issue or use this one, but after upgrading to RC2 and running the database updates, I'm noticing the following:

ENTITY/FIELD DEFINITIONS
Mismatched entity and/or field definitions
The following changes were detected in the entity type and field definitions.
File
The URL alias field needs to be uninstalled.

This was the only update I applied, so I don't see how this error could've come from anywhere else.

I tried uninstalling this module and reinstalling it (after running Cron), and the same for pathauto, but I can't seem to get rid of this message.

paintingguy’s picture

I have the same issue as @webdrips after updating to rc2

joseph.olstad’s picture

Assigned: Berdir » Unassigned
Status: Needs work » Active

reverting this, a new release to follow

  • joseph.olstad committed 9835072e on 8.x-2.x
    Revert "git commit -m 'Issue #2655844 by rutiolma, bharath-kondeti,...

  • joseph.olstad committed 6a7a41f1 on 8.x-2.x
    Issue #2655844 by joseph.olstad, paintingguy, webdrips: Remove...
joseph.olstad’s picture

Version: 8.x-2.0-rc2 » 8.x-2.x-dev
Status: Active » Postponed (maintainer needs more info)
joseph.olstad’s picture

Thanks for reporting the issues, it is reverted in rc4

https://www.drupal.org/project/file_entity/releases/8.x-2.0-rc4

webdrips’s picture

RC4 Removed the Status Report error message for me.

Steven Jones’s picture

RC4 added it back for me, this patch removed it.

@webdrips I wonder if you want a URL alias field on your files? Maybe we need to be smarter about when the base field is declared, are you also using the latest versions of pathauto?