By keymood on
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
The error is telling you that
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:
This is the full function declaration you would use. However, this can be cleaned up with a
usestatement, by doing this:Note that
usestatements are usually grouped at the top of a file (but must be under any namespace declarations), as a best practice. The benefit to declaring ausestatement is that you can then use the short form in multiple function declarations, with only a single use statement in the file:You can also alias a class name in a use statement. To understand, first look at this example:
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 usingas AwesomeFormBase:This code will work without error.
Contact me to contract me for D7 -> D10/11 migrations.
There isnt any output
Thanks for your description about namespace and using it, i declare it but currently after submit new content there is no response
It looks like you need to add
It looks like you need to add a custom form submit handler and call
See https://drupal.stackexchange.com/a/223355
Works well
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
/**