0

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).

2

The php file of the form is in a subfolder, but the class name in getForm() does not reflect this.

Change the location of the php file from

custom_module/src/Form/UserType/UserType.php

to

custom_module/src/Form/UserType.php

and

namespace Drupal\custom_module\Form\UserType;

to

namespace Drupal\custom_module\Form;
  • ok. let me try. I will let you know whether it works. – ARUN Jun 7 '17 at 9:05

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.