Since we allow multiple profiles of the same type, we also need the ability to set one as default.

This means:
1) Adding a default boolean field to the profile base fields, along with isDefault() and setDefault() methods on the class and interface.
2) Adding a "mark as default" action, similar to how enable/disable worked in https://github.com/commerceguys/commerce/commit/12c55e90f46d4e478ec872ff...
This could would need to perform an EntityQuery to get all other default profiles for the same uid and type, and set their default status to FALSE.

Comments

bojanz created an issue. See original summary.

subhojit777’s picture

Assigned: Unassigned » subhojit777
mglaman’s picture

Status: Active » Needs review
StatusFileSize
new3.21 KB
mglaman’s picture

+++ b/src/Entity/Profile.php
@@ -260,4 +265,35 @@ public function getCacheTagsToInvalidate() {
+    $profiles = \Drupal\node\Entity\Node::loadMultiple();
+
+    // Ensure that all other profiles are set to not default.
+    foreach ($profiles as $profile) {
+      if ($profile->profile_id != $this->profile_id) {
+        $profile->setDefault(0);
+      }
+    }

You're loading nodes here, and not saving the expected profile.

mglaman’s picture

Status: Needs review » Needs work

The last submitted patch, 3: add_the_ability_to_set-2636274-3.patch, failed testing.

The last submitted patch, 3: add_the_ability_to_set-2636274-3.patch, failed testing.

mglaman’s picture

Assigned: subhojit777 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.1 KB

Here is patch with fixes. First check to make sure entity being saved is default, load properly.

Status: Needs review » Needs work

The last submitted patch, 8: add_the_ability_to_set-2636274-8.patch, failed testing.

The last submitted patch, 8: add_the_ability_to_set-2636274-8.patch, failed testing.

subhojit777’s picture

Assigned: Unassigned » subhojit777

Checking the failing tests.

subhojit777’s picture

Status: Needs work » Needs review
StatusFileSize
new3.87 KB
new1.91 KB

This is throwing some plugin not found exception, not sure about this error - will have to look into it.

Status: Needs review » Needs work

The last submitted patch, 12: add_the_ability_to_set-2636274-12.patch, failed testing.

The last submitted patch, 12: add_the_ability_to_set-2636274-12.patch, failed testing.

mglaman’s picture

+++ b/config/install/profile.type.main.yml
@@ -4,4 +4,5 @@ registration: false
+status: false

+++ b/config/schema/profile.schema.yml
@@ -26,6 +26,9 @@ profile.type.*:
+    default_profile:
+      type: boolean
+      label: 'Default'

We do not need to define "default" here, because its schema is defined within base field definitions.

mglaman’s picture

Can we try it without the postSave and see if that's the issue? Help narrow it down.

subhojit777’s picture

Yes I tried without postSave, still getting the same error. I am trying to compare the new added with existing status profile field, I am checking what we are missing.

mglaman’s picture

Here's an updated patch. Moved "default" to be "primary" since default is reserved. Tried to set default value.

Randomly on UI side clearing caches fixed it.

subhojit777’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 18: add_the_ability_to_set-2636274-18.patch, failed testing.

The last submitted patch, 18: add_the_ability_to_set-2636274-18.patch, failed testing.

subhojit777’s picture

Status: Needs work » Needs review
StatusFileSize
new3.41 KB
new2.86 KB

Status: Needs review » Needs work

The last submitted patch, 22: add_the_ability_to_set-2636274-22.patch, failed testing.

The last submitted patch, 22: add_the_ability_to_set-2636274-22.patch, failed testing.

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new3.11 KB

Giving my patch a shot, seemed to pass OK on Travis.. https://travis-ci.org/mglaman/profile/builds/98537590

mglaman’s picture

StatusFileSize
new3.24 KB

Ok, so since my patch did pass, here is interdiff.

mglaman’s picture

Fix some comment mishaps.

  • mglaman committed 191b2ef on 8.x-1.x authored by subhojit777
    Issue #2636274 by mglaman, subhojit777: Add the ability to set a profile...
mglaman’s picture

Status: Needs review » Fixed

Thanks for all the help, subhojit777!

Status: Fixed » Closed (fixed)

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

subhojit777’s picture

Status: Closed (fixed) » Needs work
+++ b/src/Entity/Profile.php
@@ -259,4 +279,26 @@ class Profile extends ContentEntityBase implements ProfileInterface {
+          $profile->save();

This produces a call to itself :p

mglaman’s picture

Are you sure?

+    // Check if this profile is, or became the default.
+    if ($this->isDefault()) {
+      /** @var \Drupal\profile\Entity\ProfileInterface[] $profiles */
+      $profiles = $storage->loadMultipleByUser($this->getOwner(), $this->getType());
+
+      // Ensure that all other profiles are set to not default.
+      foreach ($profiles as $profile) {
+        if ($profile->id() != $this->id()) {
+          $profile->setDefault(FALSE);
+          $profile->save();
+        }
+      }
+    }

We're looping other other profile entities who have been marked as not default, which would miss this logic.

subhojit777’s picture

Status: Needs work » Closed (fixed)

This is working as expected. There was mistake in my code logic itself :p