want to redirect new content submission to admin/content instead going to node/id, since i use different templates for each content type and loading node/id will render like broken template, so to prevent it, decided redirect all contents to main content list page and tried function mytheme_form_alter(&$form, FormStateInterface $form_state, $form_id) {} but it returns below error:

The website encountered an unexpected error. Please try again later.
TypeError: Argument 2 passed to mytheme_form_alter() must be an instance of FormStateInterface, instance of Drupal\Core\Form\FormState given, called in localhost/web/core/lib/Drupal/Core/Theme/ThemeManager.php on line 449 in mytheme_form_alter() (line 2285 of core/includes/theme.inc).

Comments

jaypan’s picture

The error is telling you that you need to declare the full namespace to FormStateInterface. You will also need to define the $form variable as an array, so your function declaration will look like this:

HOOK_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {}

This is the full function declaration you would use. However, this can be cleaned up with a use statement, by doing this:

use Drupal\Core\Form\FormStateInterface;

HOOK_form_alter(array &$form, FormStateInterface $form_state, $form_id) {}

Note that use statements are usually grouped at the top of a file (but must be under any namespace declarations), as a best practice. The benefit to declaring a use statement is that you can then use the short form in multiple function declarations, with only a single use statement in the file:

use Drupal\Core\Form\FormStateInterface;

HOOK_form_alter(array &$form, FormStateInterface $form_state, $form_id) {}

HOOK_form_some_specific_form_alter(array &$form, FormStateInterface $form_state) {}

HOOK_form_some_other_form_alter(array &$form, FormStateInterface $form_state) {}

You can also alias a class name in a use statement. To understand, first look at this example:

namespace Drupal\example\Form;

use Some\Library\AwesomeForm;

class AwesomeForm extends AwesomeForm {}

This code will throw a fatal error with a (very confusingly worded) error due to useing two classes with the same name but different namespaces. In this case, you can alias the namespace using as AwesomeFormBase:

namespace Drupal\example\Form;

use Some\Library\AwesomeForm as AwesomeFormBase;

class AwesomeForm extends AwesomeFormBase {}

This code will work without error.

Contact me to contract me for D7 -> D10/11 migrations.

keymood’s picture

Thanks for your description about namespace and using  it, i declare it but currently after submit new content there is no response

function mytheme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

    var_dump($form_state);

}
wombatbuddy’s picture

It looks like you need to add a custom form submit handler and call 

$form_state->setRedirect($route_name);

See https://drupal.stackexchange.com/a/223355

keymood’s picture

Thanks @wombatbuddy perfect as usuall

for those who might need this approach, i used below codes to redirect to a route name or specific url

// route name
$form_state->setRedirect($route_name);
// url
$url = Drupal\core\Url::fromUserInput('/admin/content');
$form_state->setRedirectUrl($url);
wombatbuddy’s picture

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_BASE_FORM_ID_alter() for node form.
 */
function my_module_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $node = $form_state->getFormObject()->getEntity();
  
  if ($node->bundle() == 'article' && $node->isNew()) {
    $form['actions']['submit']['#submit'][] = 'my_module_form_submit';
  }
}

/**
 * Custom form submit handler.
 */
function my_module_form_submit(&$form, FormStateInterface $form_state) {
  $form_state->setRedirect('system.admin_content');
}