1

I am creating a custom entity called user_profile. Each logged in user will have a user profile. I want to be able to add and edit the user_profile entity from the same URL.

The example module provided at

Creating a Content Entity Type in Drupal 8

shows adding and editing with the same form but from different URLs.

I want to be able to create the profile when the profile does not exist and edit the profile when the profile does exist from the same URL like /user/3/my-profile

2
2

You can create a controller in its simplest form.

In my_module.routing.yml :

my_module.profile_controller_profile:
  path: '/user/{user}/my-profile'
  defaults:
    _controller: '\Drupal\my_module\Controller\ProfileController::profile'
    _title: 'Profile'
  requirements:
    _permission: 'edit profile'
    user: \d+

In ProfileController.php :

<?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\my_module\Entity\Profile;
use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class ProfileController.
 */
class ProfileController extends ControllerBase {

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new ProfileController object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
   */
  public function __construct(
    EntityTypeManagerInterface $entity_type_manager,
    EntityFormBuilderInterface $entity_form_builder
  ) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFormBuilder = $entity_form_builder;
  }


  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager'),
      $container->get('entity.form_builder')
    );
  }

  /**
   * @param \Drupal\user\Entity\User $user
   *
   * @return array
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function profile(User $user) {
    $profiles = $this->entityTypeManager->getStorage('profile')->loadByProperties(
      ['user_id' => $user->id()]
    );
    if ($profiles) {
      $profile = array_pop($profiles);
      $action = 'edit';
    } else {
      $profile = Profile::create([
        'user_id' => $user->id(),
      ]);
      $action = 'add';
    }
    return $this->entityFormBuilder->getForm($profile, $action);
  }

}

You can choose to redirect after submitting the form, etc.

0
0

That's simple. In your form's build() function check for an existing entity for that user. If there is one, prefill the fields (set #default_value). Maybe add the existing entity-id as hidden field (if any).

In your form's submit() check again for an existing entity (for security reasons) or use the hidden entity-id. Then decide in your code if you create an new entity or modify the existing one.

1
  • That would work for a custom form. This is the entity form for a custom entity. The answer by @Condutiarii works
    – anoopjohn
    Dec 6 '19 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.