public function userProfileForm(RouteMatchInterface $route_match, UserInterface $user, ProfileTypeInterface $profile_type) { $profile = $this->entityManager()->getStorage('profile')->create([ 'uid' => $user->id(), 'type' => $profile_type->id(), ]); return $this->entityFormBuilder()->getForm($profile, 'add', ['uid' => $user->id(), 'created' => REQUEST_TIME]); }
/** * Provides profile create form. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param \Drupal\user\UserInterface $user * The user account. * @param \Drupal\profile\Entity\ProfileTypeInterface $profile_type * The profile type entity for the profile. * * @return array * Returns form array. */ public function userProfileForm(RouteMatchInterface $route_match, UserInterface $user, ProfileTypeInterface $profile_type) { /** @var \Drupal\profile\Entity\ProfileType $profile_type */ /** @var \Drupal\profile\Entity\ProfileInterface|bool $active_profile */ $active_profile = $this->entityTypeManager()->getStorage('profile')->loadByUser($user, $profile_type->id()); // If the profile type does not support multiple, only display an add form // if there are no entities, or an edit for the current. if (!$profile_type->getMultiple()) { // If there is an active profile, provide edit form. if ($active_profile) { return $this->editProfile($route_match, $user, $active_profile); } // Else show the add form. return $this->addProfile($route_match, $user, $profile_type); } else { $build = []; // If there is no active profile, display add form. if (!$active_profile) { return $this->addProfile($route_match, $user, $profile_type); } $build['add_profile'] = Link::createFromRoute($this->t('Add new @type', ['@type' => $profile_type->label()]), "entity.profile.type.{$profile_type->id()}.user_profile_form.add", ['user' => \Drupal::currentUser()->id(), 'profile_type' => $profile_type->id()])->toRenderable(); // Render the active profiles. $build['active_profiles'] = ['#type' => 'view', '#name' => 'profiles', '#display_id' => 'profile_type_listing', '#arguments' => [$user->id(), $profile_type->id(), 1], '#embed' => TRUE, '#title' => $this->t('Active @type', ['@type' => $profile_type->label()]), '#pre_render' => [['\\Drupal\\views\\Element\\View', 'preRenderViewElement'], 'profile_views_add_title_pre_render']]; return $build; } }
/** * Create a user, and optionally a profile. * * @param \Drupal\profile\Entity\ProfileTypeInterface $profile_type * A profile type for the created profile entity. * @param \Drupal\user\UserInterface $user * A user to create a profile. * * @return \Drupal\profile\Entity\ProfileInterface * A profile for a user. */ protected function createProfile(ProfileTypeInterface $profile_type, UserInterface $user) { $profile = Profile::create(['type' => $profile_type->id(), 'uid' => $user->id()]); $profile->save(); return $profile; }