Account created on 9 February 2018, over 6 years ago
#

Recent comments

Permissions didn't have an effect for us.

The issue seems to be that \Drupal\profile\Plugin\Field\FieldWidget\ProfileFormWidget::saveProfiles isn't getting called by \Drupal\Core\Form\FormSubmitter::executeSubmitHandlers.
And even if it did get called, Commerce's form is an instance of \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\MultistepDefault which doesn't implement \Drupal\Core\Entity\EntityFormInterface which this Profile module is expecting since it calls ->getEntity() on this line:

$account = $form_state->getFormObject()->getEntity()

Not sure if this is the Profile module's issue or Commerce's, but it needs fixing either way!

So what I did was add a custom submit handler and just tweak that a little so it works with Commerce's \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\MultistepDefault form object.

This seems to work so far :

use Drupal\Core\Form\FormStateInterface;
use Drupal\profile\Entity\ProfileInterface;

function MYMODULE_form_commerce_checkout_flow_multistep_default_alter(&$form, FormStateInterface $form_state, $form_id)
{
    if ($form['#step_id'] === 'login') {
        // fixes profile uid getting set to 0 for commerce registration form
        $form['#submit'][] = function (array $form, FormStateInterface $form_state) {
            $uid = $form_state->get('logged_in_uid');

            if (!$uid) {
                return;
            }

            $profiles = $form_state->get('profiles');

            foreach ($profiles as $profile) {
                assert($profile instanceof ProfileInterface);
                $profile->setOwnerId($uid);
                $profile->setPublished();
                $profile->save();
            }
        };
    }
Production build 0.69.0 2024