Problem/Motivation

The "classes" field on Styles entities uses a multivalued textfield with some ajax functionality

It's quite clumsy to remove classes, despite what the help text might make you think.

Also, the use of ajax makes functional testing more difficult. It also makes functional testing more important since it's custom ajax functionality rather than standard form API stuff.

Proposed resolution

It would make use and maintenance of this project easier if the ajax stuff were removed in favor of using a single form element to hold the multiple classes.

The single form element could be a textfield with space-delimited classes or a textarea with line-break delimited classes.

User interface changes

Simpler form

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

danflanagan8 created an issue. See original summary.

akshay_d’s picture

Assigned: akshay_d » Unassigned
Status: Active » Needs review
FileSize
3.18 KB
112.02 KB

Hi team,
I have updated the entity style classes field to have simpler form by updating the ajax enabled fieldset field to textfield.

Please review the PR

Thanks

danflanagan8’s picture

Status: Needs review » Needs work

Nice work, @akshay_d

This worked well in m y manual testing, but there are still a few changes I'd like to see.

First, I have a couple nitpicky things from the form build:

+++ b/src/Form/StylesForm.php
@@ -16,12 +16,7 @@ class StylesForm extends EntityForm {
+    $classes = !empty($styles->get('classes')) ? $styles->get('classes') : NULL;

I think I'd rather see this:

$classes = $styles->get('classes') ?? [];

And then later we can implode without any additional logic:

+++ b/src/Form/StylesForm.php
@@ -42,38 +37,14 @@ class StylesForm extends EntityForm {
+      '#default_value' => $classes ? implode(' ', $classes) : '',

That could be changed to:

'#default_value' => implode($classes)

And then I'd like to see the save method approached in a different way. We should override the parent's copyFormValuesToEntity method and deal with setting values in there. I think it would look like this:

  /**
   * {@inheritdoc}
   */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    foreach ($values as $key => $value) {
      if ($key === 'classes') {
        $entity->set('classes', explode(' ', $value));
      }
      else {
        $entity->set($key, $value);
      }
    }
  }

Then the save method would look like this:

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $styles = $this->entity;
    $status = $styles->save();

    switch ($status) {
      case SAVED_NEW:
        $this->messenger()->addStatus($this->t('Created the %label Styles.', [
          '%label' => $styles->label(),
        ]));
        break;

      default:
        $this->messenger()->addStatus($this->t('Saved the %label Styles.', [
          '%label' => $styles->label(),
        ]));
    }
    $form_state->setRedirectUrl($styles->toUrl('collection'));
    return $status;
  }

Note we're returning the status at the end which is something we are forgetting to do at the moment.

This is a much better logical division of labor. Now the save method is only handling saving, messaging, and redirecting, which is what the docbloc says it should do.

And now the copyFormValuesToEntity will do what it says it does, and do it correctly the first time.

akshay_d’s picture

Status: Needs work » Needs review
FileSize
3.94 KB
2.05 KB

Thanks @danflanagan8 for reviewing the patch.

Also, I made the requested change please re-review.

danflanagan8’s picture

Status: Needs review » Needs work

Looks good, @akshay_d. Thanks for the updates!

I just have one nit. It looks like the word "or" is missing from this sentence:

+++ b/src/Form/StylesForm.php
@@ -42,38 +38,14 @@ class StylesForm extends EntityForm {
+      '#description' => $this->t('Add one more classes by delimiting with space.'),

Can you change this to read, "Add one or more space-delimited classes."

akshay_d’s picture

Status: Needs work » Needs review
FileSize
3.94 KB
488 bytes

@danflanagan8 Thanks for the detailed review.

Also, Updated the patch please review.

danflanagan8’s picture

Thanks, @akshay_d!

Here's an updated patch with tests. Ease of test coverage, after all, was part of the motivation here.

Status: Needs review » Needs work

The last submitted patch, 7: interdiff-6-7.patch, failed testing. View results

danflanagan8’s picture

Status: Needs work » Fixed

Ha! The interdiff mostly passed. :)

Status: Fixed » Closed (fixed)

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