Problem/Motivation

In #2785147: [META] Introduce a DiffLayout plugin system we are adding a layout plugin system thus the user should be able to sort the plugins to define which one should be displayed as default.

Proposed resolution

Add a sort table in the settings form. Add tests.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

yongt9412 created an issue. See original summary.

johnchque’s picture

Assigned: Unassigned » johnchque

Let's try this :)

johnchque’s picture

Status: Active » Needs review
StatusFileSize
new4.38 KB

Made some changes, not really sure how to set a default layout since we are setting the default one in the routing file.

Status: Needs review » Needs work

The last submitted patch, 3: let_the_user_sort-2790841-3.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new4.39 KB
new453 bytes

Sorry, fixed the schema.

miro_dietiker’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

That's cool, but we need strict test coverage.

The most important purpose of the weight is to move one item to the first place and thus make it the default.
We need to check that the button displays the plugins in the right order.
And we need to check that disabled plugins go away and are not accessible through the route.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new7.57 KB
new4.29 KB

Added initial tests. Still not really sure how to set the new layout filter as default.

Status: Needs review » Needs work

The last submitted patch, 7: let_the_user_sort-2790841-7.patch, failed testing.

miro_dietiker’s picture

Yeah then we can also split introducing weight stuff from the default as two separate issues.
In the worst case we can really also separately consider a/the default setting.

johnchque’s picture

Followup created.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new8.31 KB
new4.94 KB

Made some other changes. The dropdown button now displays the plugins sorted. Added the "none selected, all selected". Fixed the tests. Define the default plugin will be done in the followup.

miro_dietiker’s picture

Status: Needs review » Needs work
  1. +++ b/src/DiffLayoutManager.php
    @@ -57,4 +57,55 @@ class DiffLayoutManager extends DefaultPluginManager {
    +   * Gets the applicable plugin options for a given field.
    ...
    +   * Loop over the plugins that can be applied to the field and builds an array
    +   * of possible plugins based on each plugin weight.
    

    What field?

  2. +++ b/src/DiffLayoutManager.php
    @@ -57,4 +57,55 @@ class DiffLayoutManager extends DefaultPluginManager {
    +    $plugins = $this->config->get('general_settings' . '.' . 'layout_plugins');
    ...
    +    else {
    +      $plugins = $this->getDefinitions();
    +      foreach ($plugins as $key => $value) {
    +        $plugin_options[$key] = $value['label'];
    +      }
    

    We could simply force population of the settings on install time and then drop this else on runtime.
    Every line less here helps.

  3. +++ b/src/DiffLayoutManager.php
    @@ -57,4 +57,55 @@ class DiffLayoutManager extends DefaultPluginManager {
    +    // If layout settings have been saved used them, otherwise load all the
    +    // plugins available.
    

    Not true.

  4. +++ b/src/DiffLayoutManager.php
    @@ -57,4 +57,55 @@ class DiffLayoutManager extends DefaultPluginManager {
    +      // If any plugin is selected, all selected.
    +      if ($plugin_options == []) {
    +        $plugin_options = $plugins_order;
    

    I would skip this any=all part. It is only needed to have something grow if instances are created often. Make sure enabled is the default.

  5. +++ b/src/Form/GeneralSettingsForm.php
    @@ -58,6 +58,57 @@ class GeneralSettingsForm extends ConfigFormBase {
    +        'enabled' => $config->get('general_settings' . '.' . 'layout_plugins')[$id]['enabled'] ?: FALSE,
    +        'weight' => $config->get('general_settings' . '.' . 'layout_plugins')[$id]['weight'] ?: $weight,
    

    Can't see where those are saved?

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new8.46 KB
new3.75 KB

Fixed based on comments above, I think this might break if we disable the classic layout.

Status: Needs review » Needs work

The last submitted patch, 13: let_the_user_sort-2790841-13.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review

Not really sure if I should enable all the plugins in an update function.

miro_dietiker’s picture

Status: Needs review » Needs work

Getting the options reads straight forward now. So this seems fine.
Enabling all makes sense IMHO, it's how it is now.

What breaks if we disable classic layout?
Test fails are uncool. ;-)

johnchque’s picture

It breaks because it is set in the routing.yml file as default one.

miro_dietiker’s picture

Yeah then sure no default like that and make the method negotiate the default in code. That sounds pretty straight forward.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new7.42 KB
new13.63 KB

True, now it doesn't break if classic is disabled. Also added tests. Works nice IMHO.

Status: Needs review » Needs work

The last submitted patch, 19: let_the_user_sort-2790841-19.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new15.5 KB
new1.87 KB

Also fixed views. :)

Status: Needs review » Needs work

The last submitted patch, 21: let_the_user_sort-2790841-21.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new17.43 KB
new1.92 KB

This should fix all tests.

miro_dietiker’s picture

Status: Needs review » Needs work
  1. +++ b/src/Form/GeneralSettingsForm.php
    @@ -58,6 +86,57 @@ class GeneralSettingsForm extends ConfigFormBase {
    +    $layout_plugins = $this->diffLayoutManager->getDefinitions();
    +    $weight = count($layout_plugins) + 1;
    ...
    +        'weight' => $config->get('general_settings' . '.' . 'layout_plugins')[$id]['weight'] ?: $weight,
    

    Weights are hardcoded. You make the loop start at a position that depends the plugin count. That doesn't allow to hardcode things.

    BTW Isn't there a standard method that is called on submit that reorders weight?

  2. +++ b/src/Tests/AdminFormsTest.php
    @@ -70,4 +70,71 @@ class AdminFormsTest extends DiffTestBase {
    +    // Create a node with a revision.
    +    $edit = [
    +      'title[0][value]' => 'great_title',
    +      'body[0][value]' => '<p>great_body</p>',
    +    ];
    +    $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
    +    $this->clickLink('Edit');
    +    $edit = [
    +      'title[0][value]' => 'greater_title',
    +      'body[0][value]' => '<p>greater_body</p>',
    +    ];
    +    $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
    

    Hm, this reads so much repetitive, as if we need to do it again and again for every tiny test.
    Can you quickly check tests and if it makes sense to prepare this better?
    I think it belongs to the DiffTestBase.

  3. +++ b/src/DiffLayoutManager.php
    @@ -57,4 +57,43 @@ class DiffLayoutManager extends DefaultPluginManager {
    +    list($plugin) = array_keys($plugins);
    +    return $plugin;
    

    You should use reset to pick the first.

miro_dietiker’s picture

OK simple solution:

  1. +++ b/config/install/diff.settings.yml
    @@ -3,3 +3,13 @@ general_settings:
    +      weight: -50
    ...
    +      weight: -49
    ...
    +      weight: -48
    

    I'd start at 0.

  2. +++ b/src/Form/GeneralSettingsForm.php
    @@ -58,6 +86,57 @@ class GeneralSettingsForm extends ConfigFormBase {
    +    $weight = count($layout_plugins) + 1;
    

    Let's take max() +1

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new17.4 KB
new1.42 KB

Fixed small stuff. I think it can be better to refactor the tests in another issue, the code pointed is used a lot so better to change it all. :)

Status: Needs review » Needs work

The last submitted patch, 26: let_the_user_sort-2790841-26.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new17.4 KB
new548 bytes

AFAIK the max can be used when the weight is already set. Should it be better to keep this?

Status: Needs review » Needs work

The last submitted patch, 28: let_the_user_sort-2790841-28.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new17.4 KB
new495 bytes

hmm small mistake.

Status: Needs review » Needs work

The last submitted patch, 30: let_the_user_sort-2790841-30.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new17.48 KB
new766 bytes

Small thing that need to be changed. :)

miro_dietiker’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

OK committing this. But i'm not happy with count() as it can produce clashes when new plugins are added and custom weights are in place.

Status: Fixed » Closed (fixed)

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