The clone does appear to be successful, but:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function toArray() on null in Drupal\contact_storage\Form\ContactFormCloneForm->save() (line 131 of modules/contrib/contact_storage/src/Form/ContactFormCloneForm.php).
Drupal\contact_storage\Form\ContactFormCloneForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 585)
Drupal\Core\Form\FormBuilder->processForm('contact_form_clone_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('contact_form_clone_form', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Comments

John Pitcairn created an issue. See original summary.

znak’s picture

Assigned: Unassigned » znak
jibran’s picture

Status: Active » Postponed (maintainer needs more info)

This works just fine for me.

johnpitcairn’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

Thanks, it's working here now too. Might have been interacting with something else. Closing.

svetoslav.dragoev’s picture

Status: Closed (cannot reproduce) » Active

Hi,
We are currently using Contact Storage module, version: '8.x-1.0-beta9' and we ended up with the same error as described above. I had a little digging and found core of the problem.

When you create a Contact form initially, if you don't go through Manage form display and press Save, this configuration never gets stored as a record in the Database - key_value table, example:
collection: config.entity.key_store.entity_form_display; name: uuid:3305cf92-35d8-4085-86ce-38e96f733b31; ....

Because of that problems emerged when you use the Contact storage module to clone an entity, a form.
So when you create a new form, just Insert - Save and that's it, then you start cloning, you end up with the following error:
Fatal error: Call to a member function toArray() on a non-object in /home/duwcy/www/modules/contact_storage/src/Form/ContactFormCloneForm.php on line 124....

Which is caused of the fact that inside ContactFormCloneForm file - save method is executing the following:

    // Clone the entity form display.
    $display = EntityFormDisplay::load('contact_message.' . $original_id . '.default');
    EntityFormDisplay::create([
      'bundle' => $contact_form->id(),
      'uuid' => NULL,
    ] + $display->toArray())->save();

Important part is here:
$display->toArray())->save()

So when Form Display is not being saved, because we haven't opened that form to press the button and trigger the insert, it does not exist in the database and naturally not being found. That leaves $display variable empty, but not validated before being used as an object, which causes a fatal error.

Even if the reason is in the core for not saving Form Display data, I think a fix should be applied here, a simple validation should do the work:

    // Clone the entity form display.
    $display = EntityFormDisplay::load('contact_message.' . $original_id . '.default');
    if ($display) {
        EntityFormDisplay::create([
          'bundle' => $contact_form->id(),
          'uuid' => NULL,
        ] + $display->toArray())->save();
    }

We've already found that even when the error is triggered, all other data is being copied, so the form is working, meaning it could get along even without the `EntityFormDisplay`, so we can continue the process and don't throw errors or revert it.

And one more thing in this same file and same functionality is duplicated:

   // Clone the entity form display.
    $display = EntityFormDisplay::load('contact_message.' . $original_id . '.default');
    EntityFormDisplay::create([
      'bundle' => $contact_form->id(),
      'uuid' => NULL,
    ] + $display->toArray())->save();

    // Clone the entity view display.
    $display = EntityViewDisplay::load('contact_message.' . $original_id . '.default');
    EntityViewDisplay::create([
      'bundle' => $contact_form->id(),
      'uuid' => NULL,
    ] + $display->toArray())->save();

As you can see, it's absolutely the same. Line 119 - 131: http://cgit.drupalcode.org/contact_storage/tree/src/Form/ContactFormClon....

Best regards,
Svetoslav

vacho’s picture

@svetoslav.dragoev great job debugging the issue. I founded the same problem for 8.x-1.0 version.

I fix simply loading default configurations for this files:
core.entity_form_display.contact_message.contact.default.yml
core.entity_view_display.contact_message.contact.default.yml

I am thinking that maybe it is a core contact module issue too.

sinn’s picture

Assigned: znak » Unassigned
Status: Active » Needs review
StatusFileSize
new2.07 KB

> As you can see, it's absolutely the same. Line 119 - 131:
No, lines are different. In the beginning we create EntityFormDisplay instance and then EntityViewDisplay instance.

> I am thinking that maybe it is a core contact module issue too.
Config entities are created without view or form display configurations by default (it is true and for Vocabulary entity, for example). Based on that I think we should just add condition to check whether configurations exist or not during cloning.

Refactored test that shows the issue is attached. The main issue here was that code below creates entity view and entity form display configurations that by default is absent:
$this->fieldUIAddNewField('admin/structure/contact/manage/test_id', 'text_field', 'Text field', 'text');

Status: Needs review » Needs work

The last submitted patch, 7: contact_storage-test_shows_issue-2871305-7-D8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

sinn’s picture

Status: Needs work » Needs review
StatusFileSize
new3.32 KB
new1.25 KB

Please review the solution.

andras_szilagyi’s picture

Status: Needs review » Reviewed & tested by the community

#9 solved the issue for me

  • Berdir committed 4b1b26a on 8.x-1.x authored by sinn
    Issue #2871305 by sinn: Error when cloning
    
berdir’s picture

Status: Reviewed & tested by the community » Fixed

Committed.

  • Berdir committed 4aa06ac on 8.x-1.x
    Issue #2871305 by sinn, Berdir: Remove unnecessary assertion method that...

Status: Fixed » Closed (fixed)

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