Problem/Motivation

Just like in #2094499: Convert "Manage (form) display" forms to be the official entity edit forms for EntityView[Form]Display objects and #2446869: Convert the "Field storage edit" form to an actual entity form, we need to convert this Field UI form to D8.

Proposed resolution

Do it.

Remaining tasks

Review #2448357: Config entity forms need to have access to an updated entity object at all times first.

User interface changes

Nope.

API changes

The 'field form wrapper is removed from that form.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task because the current form is not broken.
Prioritized changes The main goal of this issue is to convert one Field UI form to the new API available in Drupal 8 (the EntityForm base class).
Disruption Minor disruption for contrib modules that alter the "Field edit" Field UI form.

Comments

amateescu’s picture

Status: Active » Needs review
StatusFileSize
new42.37 KB
amateescu’s picture

StatusFileSize
new39.45 KB

Now rolled with -M25% because the form class changes a lot.

The last submitted patch, 1: 2448503.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 2: 2448503-2.patch, failed testing.

amateescu’s picture

Status: Needs work » Needs review
StatusFileSize
new39.53 KB

The blocker went in. Hopefully, we won't have any other test fails :)

Status: Needs review » Needs work

The last submitted patch, 5: 2448503-5.patch, failed testing.

amateescu’s picture

Status: Needs work » Needs review
StatusFileSize
new47.65 KB
new10.27 KB

Fixed all that.

Status: Needs review » Needs work

The last submitted patch, 7: 2448503-7.patch, failed testing.

jibran’s picture

Some awesome work. Just two nits.

  1. +++ b/core/modules/field_ui/src/Form/FieldConfigEditForm.php
    @@ -2,149 +2,97 @@
    +    $bundles = entity_get_bundles($this->entity->getTargetEntityTypeId());
    

    We have $this->entityManager in EntityForm we can replace this with $this->entityManager->getBundleInfo($entity_type);

  2. +++ b/core/modules/field_ui/src/Form/FieldConfigEditForm.php
    @@ -2,149 +2,97 @@
    +    $ids = (object) array('entity_type' => $this->entity->getTargetEntityTypeId(), 'bundle' => $this->entity->bundle, 'entity_id' => NULL);
    

    Please change this to multi line.

yched’s picture

Aweome, just like #2446869: Convert the "Field storage edit" form to an actual entity form, this removes a lot of weirdness.

  1. +++ b/core/lib/Drupal/Core/Entity/EntityForm.php
    @@ -165,7 +165,7 @@ public function processForm($element, FormStateInterface $form_state, $form) {
         // Rebuild the entity if #after_build is being called as part of a form
         // rebuild, i.e. if we are processing input.
    -    if ($form_state->isProcessingInput()) {
    +    if ($form_state->isRebuilding() && $form_state->isProcessingInput()) {
    

    This was just added by #2448357: Config entity forms need to have access to an updated entity object at all times - but we actually need both checks ? The comment above needs to be adjusted then ?

  2. +++ b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php
    @@ -104,8 +104,16 @@ public function defaultValuesFormSubmit(array $element, array &$form, FormStateI
    -      $ids[] = $properties['target_id'];
    +      if (!empty($properties['target_id'])) {
    +        $ids[] = $properties['target_id'];
    +      }
    

    What would be a case of having a $default_values[N] that doesn't have a 'target_id' ?
    (and how is that revealed by / related to this patch ?)

  3. +++ b/core/lib/Drupal/Core/Field/FormatterInterface.php
    @@ -19,7 +19,7 @@
    -   * Invoked from \Drupal\field_ui\Form\FieldEditForm to allow
    +   * Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow
    
    +++ b/core/lib/Drupal/Core/Field/WidgetInterface.php
    @@ -25,7 +25,7 @@
    -   * Invoked from \Drupal\field_ui\Form\FieldEditForm to allow
    +   * Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow
    

    HEAD is actually lying anyway - the settingsForm() for widgets and formatters are invoked by the Display forms, right ?

  4. +++ b/core/modules/comment/src/Tests/CommentNonNodeTest.php
    @@ -392,14 +392,14 @@ function testCommentFunctionality() {
    -      'field[settings][anonymous]' => COMMENT_ANONYMOUS_MAY_CONTACT,
    +      'settings[anonymous]' => COMMENT_ANONYMOUS_MAY_CONTACT,
    

    <3, like in the other issue :-D

  5. +++ b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php
    @@ -200,13 +197,9 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
       public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) {
    -    if ($form_state->hasValue('field')) {
    -      $form_state->unsetValue(array('field', 'settings', 'handler_submit'));
    -      $form_state->get('field')->settings = $form_state->getValue(['field', 'settings']);
    -
    -      $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($form_state->get('field'));
    -      $handler->validateConfigurationForm($form, $form_state);
    -    }
    +    $field = $form_state->getFormObject()->getEntity();
    +    $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
    +    $handler->validateConfigurationForm($form, $form_state);
       }
    

    Cool indeed !

  6. +++ b/core/modules/field_ui/src/Form/FieldConfigEditForm.php
    @@ -152,55 +100,86 @@ public function buildForm(array $form, FormStateInterface $form_state, FieldConf
    +  protected function actions(array $form, FormStateInterface $form_state) {
    +    $actions = parent::actions($form, $form_state);
    +    $actions['submit']['#value'] = $this->t('Save settings');
    +
    +
    +    if (!$this->entity->isNew()) {
    +
    +      (... determine a $url ...) 
    +
    +      $actions['delete'] = array(
    +        '#type' => 'link',
    +        '#title' => $this->t('Delete'),
    +        '#url' => $url,
    +        '#access' => $this->entity->access('delete'),
    +        '#attributes' => array(
    +          'class' => array('button', 'button--danger'),
    +        ),
    +      );
    +    }
    +
    +    return $actions;
    +  }
    

    Feels weird that this is both calling the parent and re-doing all over again what it does for the 'delete' button, with just a different $url ? Can't we just override $actions['delete']['#url'], just like we simply override $actions['submit']['#value'] above ?

    (also, double empty line)

amateescu’s picture

Status: Needs work » Needs review
StatusFileSize
new46.42 KB
new6.06 KB

#9.1 and 2: Fixed.

#10.1 and 2: Because I was dumb and I broke the default_value handling by putting it in buildEntity(). Moved it to where it should be (in submitForm()) and everything works again with the changes to EntityForm and EntityReferenceFieldItemList reverted.

#10.3: Fixed.

#10.4 and 5: Awesomesauce :D

#10.6: That's because the parent method will only generate the delete element if the entity has a 'delete-form' link template, and field_config doesn't have one :/

yched’s picture

Status: Needs review » Reviewed & tested by the community

the parent actions() method will only generate the delete element if the entity has a 'delete-form' link template, and field_config doesn't have one

Fair enough. Isn't that something we'd want to add ? :-) We do add them for view_mode & form_mode config entity types.
(probably not striclty related to this patch though...)

Related - field_ui's job of "decorate the 4 core entity types (field, field_storage, view_mode, form_mode)" seems a bit randomly split between field_ui_entity_type_build() and field_ui_entity_type_alter() 200 lines lower.
The usual rule of thumb is "hook_*_build() is to add stuff that's not there, hook_*_alter() is to modify what's already there", right ? So it should all go in field_ui_entity_type_build() ?
(also not srtricly related to this patch here - followup once this one and its sister issue are in ?)

jibran’s picture

amateescu’s picture

Isn't that something we'd want to add ? :-) We do add them for view_mode & form_mode config entity types.

We actually had them until recently, but, since we've converted most of Field UI to entity forms, they don't work anymore because Drupal\Core\Entity\Entity::urlInfo() is doing some hardcoding on link template -> route name + parameters.

Also, view modes and form modes can have link templates because they have a fixed path (e.g. /admin/structure/display-modes/form/manage/{entity_form_mode}), but field storage config, field config and entity displays have dynamic paths that vary by field_ui_base_route.

Related - field_ui's job of "decorate the 4 core entity types (field, field_storage, view_mode, form_mode)" seems a bit randomly split between field_ui_entity_type_build() and field_ui_entity_type_alter() 200 lines lower.
The usual rule of thumb is "hook_*_build() is to add stuff that's not there, hook_*_alter() is to modify what's already there", right ? So it should all go in field_ui_entity_type_build() ?

Very good point! Although, the hook_*_build() in this case is meant for "adding something new on the top level" afaik, (e.g. a new entity type definition that's not in code, for example what ECK will use), so what we're currently doing in field_ui_entity_type_build() should move to field_ui_entity_type_alter() because we're just altering the definition of already existing entity types.

yched’s picture

view modes and form modes can have link templates because they have a fixed path [...] but field storage config, field config and entity displays have dynamic paths that vary by field_ui_base_route

Right, makes sense. Thanks for the explanation :-)

Opened #2459949: Remove field_ui_entity_type_alter() and move the content to field_ui_entity_type_build() - this patch here should be good to go.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: 2448503-11.patch, failed testing.

amateescu’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new46.11 KB

Rerolled.

amateescu’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 95dc317 and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation to the issue summary.

  • alexpott committed 95dc317 on 8.0.x
    Issue #2448503 by amateescu: Convert the "Field edit" form to an actual...

Status: Fixed » Closed (fixed)

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