Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field-create: Make it easier for field types extending entity reference to add target type/bundles options (11.x) #5556

Conversation

DieterHolvoet
Copy link
Contributor

The field-create command provides two options for entity reference fields, target-type and target-bundle. Currently, these options only appear for the entity_reference field type, not field types extending it like entity_reference_revisions.

Instead of entity_reference_revisions having to duplicate all code related to those two options, I think it would be a good idea to make it possible to somehow inherit the entity_reference code.

This is how we would be able to implement those two options for entity_reference_revisions using the implementation in this MR:

<?php

namespace Drupal\entity_reference_revisions\Commands;

use Drush\Drupal\Commands\field\FieldEntityReferenceHooks;
use Symfony\Component\Console\Input\InputInterface;

class EntityReferenceRevisionsHooks extends FieldEntityReferenceHooks {

    /**
     * @hook on-event field-create-field-storage
     */
    public function hookFieldStorage(array $values, InputInterface $input): array
    {
        if ($input->getOption('field-type') === 'entity_reference_revisions') {
            $values['settings']['target_type'] = $this->getTargetType($input);
        }

        return $values;
    }

    /**
     * @hook on-event field-create-field-config
     */
    public function hookFieldConfig(array $values, InputInterface $input): array
    {
        if ($input->getOption('field-type') === 'entity_reference_revisions') {
            $values['settings']['handler_settings']['target_bundles'] = $this->getTargetBundles($input);
        }

        return $values;
    }

}

@DieterHolvoet
Copy link
Contributor Author

@weitzman does this look like a good idea to you, given the debate on whether or not to make all command classes final? If not, do you have a suggestion for another solution?

@weitzman
Copy link
Member

weitzman commented May 5, 2023

That decision is more of a "by default". It's ok if some commands deviate. Happy to proceed here however you think is best.

@weitzman
Copy link
Member

Lets remember to target 12.x as well.

@DieterHolvoet DieterHolvoet force-pushed the feature/easier-entity-reference-field-options branch from ed52b9c to 48b95a7 Compare June 2, 2023 11:21
@DieterHolvoet
Copy link
Contributor Author

This is ready. There's already a test to make sure the target-type option is asked when creating an entity reference field, so I don't think this needs extra test coverage.

@DieterHolvoet DieterHolvoet merged commit 1dee70f into drush-ops:11.x Jun 2, 2023
2 checks passed
@DieterHolvoet DieterHolvoet changed the title field-create: Make it easier for field types extending entity reference to add target type/bundles options field-create: Make it easier for field types extending entity reference to add target type/bundles options (11.x) Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants