2

I've read through other questions regarding the same error message. They all seem to end up being due to typos or the file path. I don't believe that is the case here, but we'll see. I have a working instance of the same code aside from the names, which is all the more confusing.

I enable the module below. Select place its block. And then when I hit the page, receive:

InvalidArgumentException: The form argument Drupal\terminal\Form\TerminalCliForm is not a valid form. in Drupal\Core\Form\FormBuilder->getFormId() (line 197 of core/lib/Drupal/Core/Form/FormBuilder.php).

I've verified that getFormId receives an empty form_id. I've read that this error message indicates not a bad form in those specific cases but a form file that cannot be found. I'm not certain which is the case, here.

/modules/custom/terminal/src/Form/TerminalCliForm

namespace Drupal\terminal\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class TerminalCliForm.
*/
class TerminalCliForm extends FormBase {

  /**
   * {@inheritdoc}
  */
  public function getFormId() {
    return 'terminal_cli_form';
  }

  /**
   * {@inheritdoc}
  */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#attached']['library'][] = 'terminal/terminal';
    $form['canvas'] = [
      '#type' => 'textarea',
    ];
    $form['commandline'] = [
      '#type' => 'textfield',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
  */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Nothing.
  }

  /**
   * {@inheritdoc}
  */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

}

/modules/custom/terminal/src/Plugin/Block/TerminalCliBlock - I've tried this with and without the use statement for the form class. In the other module that works correctly, it is present.

    namespace Drupal\terminal\Plugin\Block;

    use Drupal\Core\Block\BlockBase;
    use Drupal\terminal\Form\TerminalCliForm;

    /**
     * Provides a 'CliBlock' block.
     *
     * @Block(
     *   id = "terminal_cli_block",
     *   admin_label = @Translation("CLI block"),
     *   category = @Translation("Engine block")
     * )
    */

    class TerminalCliBlock extends BlockBase {

     /**
      * {@inheritdoc}
     */
     public function build() {

       $form = \Drupal::formBuilder()->getForm(TerminalCliForm::class);

       return $form;
     }
   }
8
  • Have you copy pasted the filenames (and location) or typed it? (I would bet there is a typo in the folder or file names ;))
    – rémy
    Commented Sep 1, 2021 at 15:14
  • web/modules/custom/terminal/src/Form/TerminalCliForm.php and web/modules/custom/terminal/src/Plugin/Block/TerminalCliBlock.php pasted from PhpStorm.
    – JAyenGreen
    Commented Sep 1, 2021 at 15:38
  • This file: /modules/custom/terminal/src/Form/TerminalCliForm should have the file extension .php added to it.
    – Jaypan
    Commented Sep 1, 2021 at 16:29
  • They both have a .php extension, as pasted above from PhpStorm
    – JAyenGreen
    Commented Sep 1, 2021 at 16:32
  • 2
    That did it @Clive. Clearing cache, no, but stopping and restarting docksal, yes! Please post it as an answer.
    – JAyenGreen
    Commented Sep 1, 2021 at 20:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.