All the pieces where in the question.
I will show an example on User Entity, using a module that I created
Steps will be:
- Backup !!
- Config a display Form mode in admin interface
- Create a custom module
- Set our formClass for our entity user
- Create a block class
- Add the block to your page
- Enjoy !! (backup again !! never know)
1 - Config a display Form mode
Go to this page and follow the steps. It will be really fast, like one field to input... easy !! Please keep the machine_name of the item you will create, mine was nouvea. Activate the display modes into 'Config > User parameter > manage fields > advanced settings'
2 - Create a custom module
Go to this page, not much to do here
- create a folder
- put a module_name.info.yml
- another module_name.module in it, we will use this file after
- done !
3 - Set our formClass for to our entity user
As said on this comment, you need to declare your form class for your entity User
Add into your module_name.module
<?php
/**
* Implements hook_entity_type_build().
*/
function form_display_block_entity_type_build(array &$entity_types) {
$entity_types['user']->setFormClass('nouvea', 'Drupal\user\RegisterForm');
}
More details about hook_entity_type_build()
I am using 'Drupal\user\RegisterForm' because it is the only form I could have quick and easy access to it without creating one of my own (not sure about the consequences...)
4 - Create a block class
We will just copy a class created into formblock module using user Register form.
I added this class into my 'module folder > src > Plugin > Block'
<?php
namespace Drupal\form_display_block\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
/**
* Provides a block for the coach registration form.
*
* @Block(
* id = "formblock_coach_register",
* admin_label = @Translation("Coach registration form"),
* provider = "user",
* category = @Translation("Forms")
* )
*
* Note that we set module to contact so that blocks will be disabled correctly
* when the module is disabled.
*/
class CoachRegisterBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The entity manager
*
* @var \Drupal\Core\Entity\EntityManagerInterface.
*/
protected $entityManager;
/**
* The entity form builder
*
* @var \Drupal\Core\Entity\EntityManagerInterface.
*/
protected $entityFormBuilder;
/**
* Constructs a new UserRegisterBlock plugin
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager.
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entityFormBuilder
* The entity form builder.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entityManager, EntityFormBuilderInterface $entityFormBuilder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entityManager;
$this->entityFormBuilder = $entityFormBuilder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager'),
$container->get('entity.form_builder')
);
}
/**
* Implements \Drupal\block\BlockBase::build().
*/
public function build() {
$build = array();
$account = $this->entityManager->getStorage('user') ->create(array());
$build['form'] = $this->entityFormBuilder->getForm($account, 'nouvea');
return $build;
}
/**
* {@inheritdoc}
*/
public function blockAccess(AccountInterface $account) {
return AccessResult::allowedIf($account->isAnonymous() && (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY))
->addCacheContexts(['user.roles'])
->addCacheTags(\Drupal::config('user.settings')->getCacheTags());
}
}
I replaced register by the name of my new form mode display nouvea
In build() function
$build['form'] = $this->entityFormBuilder->getForm($account, 'register');
Replaced by
$build['form'] = $this->entityFormBuilder->getForm($account, 'nouvea');
5 - Add the block to your interface
Now final step is clear your cache (drush cr or interface link) and go to add the created block (you should find the admin_label, in my case "Coach registration form") to your page using admin interface.
And hypothetically, it is working great !!