Right now we just use textfields for every element, we need to more thoroughly implement schema to form elements. There could be an existing service for this? Otherwise we can write one.

Comments

benjy created an issue. See original summary.

sam152’s picture

Version: » 8.x-1.x-dev
Assigned: Unassigned » sam152

I can have a look at this.

benjy’s picture

Project: Migrate UI » Migrate API
Version: 8.x-1.x-dev »

Moving to migrate_api

sam152’s picture

Assigned: sam152 » Unassigned
Status: Active » Needs review
StatusFileSize
new23.78 KB
new2.76 KB

Started on this, no tests and only a basic improvement pure textfields. Not sure how to approach sequences at this stage, likely some 'add another' AJAX? After writing this, part of me thinks 'MigrateProcessInterface' should just ship with a 'configForm' method as part of it's interface. Schema doesn't seem like a clean mapping to forms.

See system.site in the attached screenshot.

benjy’s picture

The content_translation module in core already converts schema to forms, we could see what they do, but i'd imagine something like this for sequence:

my_sequence:
  type: sequence
  sequence:
    type: string

$form['my_sequence'][$delta] = [
  '#type' => 'textfield',
];

With an ajax add more yes, but i'd be happy to commit this with just support for textfields and checkboxes, we can do the rest in a follow-up.

Adding a configForm method to process plugins is something we could do but i see a few disadvantages:

  1. It could be possible for the schema/data required by the plugin diverges from the config form.
  2. We'd also need to implement similar concepts for destinations and sources in the future.
benjy’s picture

Another thought, i'd accept sequence as a textfield and a description that tells you to comma separate values as a first pass at this as well.

sam152’s picture

Version: » 8.x-1.x-dev

As discussed the configForm option would be similar to the pattern used by field formatters, but it does increase the size of the interface and is potentially out of scope for the core API. If the UI can derive a suitable form without having to change the interface perhaps that's better?

sam152’s picture

StatusFileSize
new8.37 KB
new161.71 KB

This still needs a large amount of work and isn't ready for a full review. Implementation details in TypedConfigManager are causing huge headaches. The rabbit hole is deep on this one...

A more complex schema def is attached (core.entity_view_display.*.*.*).

sam152’s picture

Issue summary: View changes
StatusFileSize
new13.12 KB

Using the 'Add another' on a less insane definition:

sam152’s picture

StatusFileSize
new18.19 KB

Ready for a review, still a bit to do but improvements need to be made to the plugin manager first.

benjy’s picture

This is looking good, some feedback below, all pretty trivial. That unique ajax id problem is interesting, I'd like to take a closer look at that before committing.

  1. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,258 @@
    +    $schema_plugin_id = 'core.entity_view_display.*.*.*';
    

    Left over debug code.

  2. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,258 @@
    +    $this->processSchema($plugin->getDataDefinition(), 'root', $form, $form_state);
    ...
    +    if ($context === 'root') {
    

    Lets put "root" into a constant?

  3. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,258 @@
    +    // something more primative? Should this be the job of the manager? @todo
    

    Did we want to open a new issue for this @todo? Otherwise we can remove it.

  4. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,258 @@
    +      // This is a work around to a bad configManager implementation.
    

    s/to/for

  5. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,258 @@
    +   *   The form subtree currently being built.
    ...
    +  protected function mappingHandler($schema, $context, &$form, FormStateInterface $form_state) {
    ...
    +      $this->processSchema($mapping, $mapping_key, $form[$context], $form_state);
    

    $schema can be typehinted as an array.

    $context sounds like an array from the param docs but then it's used as the key in $form[$context] ?

  6. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,258 @@
    +    foreach (range(0, $form_state->get(['schema_form_deltas', $unique_ajax_key]) ?: 0) as $deta) {
    +      $this->processSchema($schema['sequence'], $deta, $form[$context], $form_state);
    

    s/$deta/$delta

  7. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,258 @@
    +      '#value' => 'Add another',
    

    $this->t('Add another")

  8. +++ b/src/SchemaFormBuilderInterface.php
    @@ -0,0 +1,29 @@
    +namespace Drupal\migrate_api;
    +use Drupal\Core\Form\FormStateInterface;
    

    Missing space.

Thanks again!

sam152’s picture

StatusFileSize
new18.45 KB
new9.75 KB

Thanks for the review! I have replaced the @todo’s with actionable tasks which can be turned into issues, as opposed of whinges. I've also implemented the feedback.

Feel free to put some time against the AJAX keys. Like the code mentions it works for the most complicated example I could find in core, but I could probably create a test case that breaks it at well. I think a webtest will have to do given it involves the AJAX system. Happily a phase 2 improvement for me though.

benjy’s picture

Status: Needs review » Needs work

I tested this with migrate_ui and I got a fatal error:

ResponseText: Recoverable fatal error: Argument 1 passed to Drupal\migrate_api\SchemaFormBuilder::processSchema() must be of the type array, object given, called in /var/www/app/modules/migrate_api/src/SchemaFormBuilder.php on line 79 and defined in Drupal\migrate_api\SchemaFormBuilder->processSchema() (line 95 of /var/www/app/modules/migrate_api/src/SchemaFormBuilder.php).

PHPStorm was actually reporting the same issue to me. I think it's just a bad type-hint?

sam152’s picture

StatusFileSize
new18.45 KB
new1.65 KB
new784 bytes

I think you're right. I have the same patch in migrate_ui. Could it caused by an object that implements ArrayAccess? I couldn't actually reproduce this. I've attached my migrate_ui patch to compare?

benjy’s picture

+++ b/src/SchemaFormBuilder.php
@@ -0,0 +1,256 @@
+        'callback' => 'Drupal\migrate_api\SchemaFormBuilder::sequenceHandlerAjax',
...
+    drupal_static_reset('schema_form_builder_ajax');
...
+    $key_usage = &drupal_static('schema_form_builder_ajax', []);

Also, i note we have some interesting side-effects here from the fact we aren't actually part of the form.

Using a global like would fail if we used this service to build two forms on the same page?

sam152’s picture

I believe the static can be reset as getFormArray is called. That would prevent any conflicts.

sam152’s picture

StatusFileSize
new18.48 KB
new2.79 KB

Fixes discussed.

sam152’s picture

Status: Needs work » Needs review

Testbot?

sam152’s picture

StatusFileSize
new18.25 KB
new2.85 KB

Status: Needs review » Needs work

The last submitted patch, 19: 2624746-19.patch, failed testing.

sam152’s picture

Status: Needs work » Needs review

The last submitted patch, 8: 2624746-8.patch, failed testing.

The last submitted patch, 10: 2624746-10.patch, failed testing.

The last submitted patch, 12: 2624746-12.patch, failed testing.

The last submitted patch, 14: 2624746-14.patch, failed testing.

The last submitted patch, 14: migrate-ui.patch, failed testing.

The last submitted patch, 17: 2624746-17.patch, failed testing.

benjy’s picture

 cd /var/www/html && sudo -u www-data php /var/www/html/core/scripts/run-tests.sh  --url http://localhost/checkout --dburl mysql://drupaltestbot:drupaltestbotpw@drupaltestbot-db-mysql-5-5/jenkins_default_49230  --color --keep-results --color --concurrency 31 --sqlite /var/www/html/results/simpletest.sqlite --php /opt/phpenv/shims/php --directory modules/migrate_api
10:48:42 Command created as exec id dd9bd7fa
10:48:42   ERROR: No valid tests were specified.
10:48:42 HTTP/1.1 200 OK

What happens if you run that command locally, does run-tests.sh pick them up?

Status: Needs review » Needs work

The last submitted patch, 19: 2624746-19.patch, failed testing.

benjy’s picture

The patch in #19 still has the following issues for me:

ResponseText: Recoverable fatal error: Argument 1 passed to Drupal\migrate_api\SchemaFormBuilder::processSchema() must be of the type array, object given, called in /var/www/app/modules/migrate_api/src/SchemaFormBuilder.php on line 87 and defined in

Once that's resolved i'm just going to commit this and see if we can get the tests to run that way.

I was going to fix these on commit but since we need a re-roll anyway, you may as well include them in the patch.

  1. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,262 @@
    + * Contains Drupal\migrate_api\SchemaFormBuilder.
    

    Missing backslash.

  2. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,262 @@
    +   * @param $context
    ...
    +   * @param $form
    

    Missing param types.

  3. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,262 @@
    +        $type_definition['label'] = $schema['label'];
    ...
    +        '#title' => $schema['label'],
    

    I was getting undefined indexes on schema label for some forms.

  4. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,262 @@
    +   * @param $context
    ...
    +   * @param $form
    

    Missing param types.

  5. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,262 @@
    +   * @param $context
    ...
    +   * @param $form
    

    Missing param types.

  6. +++ b/src/SchemaFormBuilder.php
    @@ -0,0 +1,262 @@
    +   * throughut schema definitions, making it very difficult to create a stable
    

    throughout*

  7. +++ b/src/SchemaFormBuilderInterface.php
    @@ -0,0 +1,35 @@
    + * Contains Drupal\migrate_api\SchemaFormBuilderInterface.
    

    Missing backslash.

  8. +++ b/src/SchemaFormBuilderInterface.php
    @@ -0,0 +1,35 @@
    +   * @param $schema_plugin_id
    

    Missing type.

  9. +++ b/tests/src/Unit/SchemaFormBuilderTest.php
    @@ -0,0 +1,248 @@
    +   * @dataProvider schemaDefinitionsWithExpectedForms
    +   *
    +   * Test that the schema form builder works.
    

    Comment should be first.

sam152’s picture

StatusFileSize
new5.66 KB
new18.39 KB

Comments from #30 attached.

benjy’s picture

Status: Needs work » Needs review

Going to commit this now.

Status: Needs review » Needs work

The last submitted patch, 31: 2624746-31.patch, failed testing.

  • benjy committed 41c2b78 on 8.x-1.x authored by Sam152
    Issue #2624746 by Sam152: Building form elements from schema
    
benjy’s picture

Status: Needs work » Fixed

The tests passed locally, the bot still seems to be having issues with them though, not sure why. If they don't pass on HEAD within 24hrs, i'll create a testbot issue.

benjy’s picture

Finally a green build: https://dispatcher.drupalci.org/job/default/49575/console

Seems you just have to commit it the first time you add tests, same as when you add new module dependencies.

sam152’s picture

Ah, useful to know. Thanks for the reviews + commit. Will look at the integration aspect for migrate_ui, but if you get to it before me, go right ahead with it.

Status: Fixed » Closed (fixed)

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