In my custom module I have created custom for like below: Path: custom_module/src/Form/UserType/UserType.php
<?php
/**
* @file
* Contains \Drupal\resume\Form\UserType.
*/
namespace Drupal\custom_module\Form\UserType;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class userType extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'user_type_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$user_types = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree('user_type');
$user_type_options = array();
foreach ($user_types as $user_type) {
$user_type_options[$user_type->tid] = $user_type->name;
}
$form['user_type'] = array(
'#type' => 'select',
'#options' => $user_type_options,
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
Then I created a block in following path: custom_module/src/Plugin/Block/UserTypeBlock.php
<?php
/**
* @file
* Contains \Drupal\custom_module\Plugin\Block\UserTypeBlock.
*/
namespace Drupal\custom_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormInterface;
/**
* Provides a 'user_type' block.
*
* @Block(
* id = "user_type_block",
* admin_label = @Translation("User Type block"),
* category = @Translation("Custom")
* )
*/
class UserTypeBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('Drupal\custom_module\Form\UserType');
return $form;
}
}
But after assign the block in to a region I'm getting following error:
InvalidArgumentException: The form argument Drupal\custom_module\Form\UserType is not a valid form. in Drupal\Core\Form\FormBuilder->getFormId() (line 190 of C:\wamp\www\www_mysite_com\core\lib\Drupal\Core\Form\FormBuilder.php).