1

I have a WelcomeForm inside my src folder in my install profile. My install profile is located inside web/profiles folder and is correctly selected by default during install.

However, I added an install task (inside my business_profile.install file) as so...

function business_profile_install_tasks_alter() {
  $new_task['install_welcome'] = [
    'display' => TRUE, 
    'type' => 'form',
    'function' => '\Drupal\business_profile\src\Form\WelcomeForm'
  ];

But the installer shows an error -

InvalidArgumentException. Form \Drupal\business_profile\src\Form\WelcomeForm is not a valid form in... ->getFormId()

My WelcomeForm extends FormBase and implements getFormId() returning a valid string. Also implements buildForm() and returns a valid $form array. Nothing wrong anywhere.And here's my WelcomeForm...

<?php

namespace Drupal\business_profile\Form;

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

class WelcomeForm extends FormBase {

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

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

    $form['#title'] = $this->t('Welcome');

    $form['actions'] = [
      '#type' => 'actions'
    ];

    $form['actions']['save'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save and continue'),
      '#button_type' => 'primary',
      '#submit' => ['::submitForm'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->setRedirect('<front>');
  }
  
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {}

}

And I can confirm the form is saved as web/profiles/business_profile/src/Form/WelcomeForm.php

What could possibly make Drupal installer not to see my Form then? Is this an autoload issue (just suspecting) - but I thought files in a selected install profile (distribution) are autoloaded?

Spent a few days on this, any help pls guys?

3
  • Showing us the code for WelcomeForm would help.
    – mona lisa
    Commented Aug 29, 2022 at 13:35
  • Right, cilefen. I've adjusted the question to include the form code. Thanks for your response.
    – ilechcod
    Commented Aug 29, 2022 at 14:16
  • The exception is thrown by this function: api.drupal.org/api/drupal/…
    – mona lisa
    Commented Aug 29, 2022 at 14:33

2 Answers 2

3

Ok, guys - I just sorted this out! But you have all been awesome, thanks a bunch! Following leymanxx suggestions, I replaced 'function' => '\Drupal\business_profile\src\Form\WelcomeForm' with 'function' => WelcomeForm::class, and then made an import on top.

I also had to add an autoload entry to my web/profiles/business_profile/composer.json file like this...

    "autoload": {
        "psr-4": {
            "Drupal\\business_profile\\": "src"
        }
    },

I then did a composer update and saw Generating autoload files message on my terminal. And bam!! Form shows up.

Now I get to design my form. Thanks a bunch @leymanxx, @cilefen, @Tritof and everyone else that helped out on this.

1

See #2965929; it's probably the class name not matching the file name. The file needs to be named WelcomeForm.php.

Please also remove the src from the function assignment. It needs to be 'function' => '\Drupal\business_profile\Form\WelcomeForm'.

3
  • The file is named WelcomeForm.php. I've confirmed this.
    – ilechcod
    Commented Aug 29, 2022 at 16:49
  • @ilechcod – Ah got it! It needs to be 'function' => '\Drupal\business_profile\Form\WelcomeForm' without the src
    – leymannx
    Commented Aug 29, 2022 at 17:26
  • Thanks leymannx - this was definitely one of the issues.
    – ilechcod
    Commented Aug 29, 2022 at 18:03

Your Answer

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

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