diff --git a/src/Form/ProfileForm.php b/src/Form/ProfileForm.php index ae2ef07..6013c05 100644 --- a/src/Form/ProfileForm.php +++ b/src/Form/ProfileForm.php @@ -29,13 +29,23 @@ class ProfileForm extends ContentEntityForm { $element = parent::actions($form, $form_state); /** @var \Drupal\profile\Entity\ProfileInterface $profile */ $profile = $this->entity; + /** @var \Drupal\profile\Entity\ProfileTypeInterface $bundle */ + $bundle = $this->entityTypeManager->getStorage('profile_type')->load($profile->bundle()); - // Add an "Activate" button. - $element['set_default'] = $element['submit']; - $element['set_default']['#value'] = t('Save and make default'); - $element['set_default']['#weight'] = 10; - $element['set_default']['#access'] = !$profile->isDefault(); - array_unshift($element['set_default']['#submit'], [$this, 'setDefault']); + // If this is a new profile, the Save button should set it as the default + // automatically. Otherwise, display a "make default" button if the profile + // type supports multiple profiles. + if ($profile->isNew() && !$bundle->getMultiple()) { + array_unshift($element['submit']['#submit'], [$this, 'setDefault']); + } + elseif ($bundle->getMultiple()) { + // Add a "Set Default" button. + $element['set_default'] = $element['submit']; + $element['set_default']['#value'] = t('Save and make default'); + $element['set_default']['#weight'] = 10; + $element['set_default']['#access'] = !$profile->isDefault(); + array_unshift($element['set_default']['#submit'], [$this, 'setDefault']); + } return $element; } diff --git a/tests/src/Functional/ProfileDefaultTest.php b/tests/src/Functional/ProfileDefaultTest.php index 1cc55f4..414384e 100644 --- a/tests/src/Functional/ProfileDefaultTest.php +++ b/tests/src/Functional/ProfileDefaultTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\profile\Functional; use Drupal\profile\Entity\Profile; +use Drupal\profile\Entity\ProfileType; use Drupal\user\Entity\User; /** @@ -62,33 +63,29 @@ class ProfileDefaultTest extends ProfileTestBase { * Tests whether profile default on edit is working. */ public function testProfileEdit() { - $types_data = [ - 'profile_type_0' => [ - 'label' => $this->randomMachineName(), - 'multiple' => TRUE, - ], - ]; - - /** @var \Drupal\profile\Entity\ProfileTypeInterface[] $types */ - $types = []; - foreach ($types_data as $id => $values) { - $types[$id] = $this->createProfileType($id, $values['label']); - } + $type = ProfileType::create([ + 'id' => $this->randomMachineName(), + 'label' => $this->randomMachineName(), + 'registration' => FALSE, + 'roles' => [], + 'multiple' => TRUE, + ]); + $type->save(); $admin_user = $this->drupalCreateUser([ 'administer profiles', 'administer users', - 'edit any ' . $types['profile_type_0']->id() . ' profile', + 'edit any ' . $type->id() . ' profile', ]); // Create new profiles. $profile1 = Profile::create($expected = [ - 'type' => $types['profile_type_0']->id(), + 'type' => $type->id(), 'uid' => $this->user1->id(), ]); $profile1->save(); $profile2 = Profile::create($expected = [ - 'type' => $types['profile_type_0']->id(), + 'type' => $type->id(), 'uid' => $this->user1->id(), ]); $profile2->setDefault(TRUE); @@ -98,6 +95,10 @@ class ProfileDefaultTest extends ProfileTestBase { $this->assertTrue($profile2->isDefault()); $this->drupalLogin($admin_user); + + $this->drupalGet($profile2->toUrl('edit-form')->toString()); + $this->assertSession()->buttonNotExists('Save and make default'); + $this->drupalGet($profile1->toUrl('edit-form')->toString()); $this->submitForm([], 'Save and make default');