Problem/Motivation

Users might require the dynamic creation of taxonomy terms during the Media editting process.

However the default field settings do not have the "Create referenced entities if they don't already exist" (auto_create) option available to configure.

Steps to reproduce

Attempt to create/edit a Media entity, but also attempt to put in a new Directory Taxonomy term.

Proposed resolution

Add a "Create referenced taxonomy term if they don't already exist" option in the Media Directory configuration page which updates the existing media_directories_entity_base_field_info() hook.

Something along the lines of:

$fields['directory']
  ->setSetting('handler_settings', [
    'target_bundles' => [$vocabulary => $vocabulary],
    'auto_create' => true,
    'auto_create_bundle' => $vocabulary,
]);

Remaining tasks

Provide patch/issue fork.

User interface changes

There will be a new form configuration to enable/disable the ability to create new taxonomy terms as necessary.

API changes

N/A

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

codebymikey created an issue. See original summary.

ytsurk’s picture

Status: Active » Needs work

Thank you for the input.

This definitely would be a nice feature IMO. And maybe even easily implementable. I'll have a look into this when I find some time ...

codebymikey’s picture

Status: Needs work » Needs review
StatusFileSize
new4.04 KB

Attached an initial draft of this implementation. Please feel free to review, and update as necessary.

Also added a todo regarding using config events to trigger the cache clear side effects.
Making changes to the configuration via other means e.g. programmatic approach or via Drush should also trigger the invalidation of the specific caches. In this case, probably something like this:

\Drupal::service('entity_type.manager')->clearCachedDefinitions();
\Drupal::service('entity_type.bundle.info')->clearCachedBundles();
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
codebymikey’s picture

One slight UX limitation at the moment is that if the user does add a new taxonomy term dynamically from the media_directory_ui browser, it doesn't refresh the list of folders, but for my current MVP, this implementation is suitable for now.

tinytina’s picture

+1 for this feature.

Tried diff from #4 with media_directories 2.1.0-beta2 and core 10.0.9, seems to work pretty well.