Drupal 8 - Disable view of structure - drupal

I'm using drupal 8, and i want to disable the view mode of node, and let user on the edit page after saving a node, but i founded nothing to do that.
Is someone have a solution ?
Thanks.

You should add hook_form_alter() and alter submit action if you detect that node saving form is displayed. Something like:
use Drupal\Core\Form\FormStateInterface;
//hook_form_alter
function hook_form_alter(&$form, $form_state, $form_id) {
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'callback_submit';
}
}
}
function callback_submit($form, FormStateInterface $form_state) {
$form_state->setRedirect('custom.page');
}
For details check this page:
https://drupal.stackexchange.com/questions/163626/how-to-perform-a-redirect-to-custom-page-after-node-save-or-delete

Related

How to change the title/branding in the drupal user registration form

I would like to know how to change the branding of the Drupal user registration form. I don't remember how I changed that earlier. Any help would be much appreciated. Thank you in advance.
please check the image
You can override the form with hook_form_alter.
In the template.php of your theme, you should add:
function your_module_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user_register') {
drupal_set_title(t('Create student Details'));
}
elseif ($form_id == 'user_pass') {
drupal_set_title(t('Request new password'));
}
elseif ($form_id == 'user_login') {
drupal_set_title(t('Log in'));
}
}

Is there a way to override user_admin_account?

I need to override the user_admin_account function (which list users in backoffice).
Is there a way to do that without modify the core? I don't see any hook to do that.
That function is a form so you can implement hook_form_alter() in a custom module (or even a theme as it's an alter hook).
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_admin_account') {
$form['something']['#default_value'] = 'something else';
}
}

Display a thanks page in Drupal quiz module

I have the Quiz module installed in my drupal website. My client want to see a thanks page rather than a Quiz Result page, once the Quiz is completed.
I haven't really used Quiz module soecifically, but I surmise can pass it a form state redirect using hook_form_alter. Something like:
function MODULE_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == 'form_id')
{
$form['actions']['submit']['#submit'][] = 'MODULE_form_id_submit_handler';
}
}
Then catch that with a custom submit handler:
function MODULE_form_id_submit_handler($form, &$form_state)
{
$form_state['redirect'] = 'path/to/thanks/page';

How to make user email non-required upon registration in drupal 7?

How to make user email non-required (optional) upon registration in drupal 7?
I was able to make a custom module. It's really similar to sharedemail.
<?php
function noemail_form_user_register_form_alter(&$form, &$form_state, $form_id) {
noemail_form_user_account_form_alter($form, $form_state, $form_id);
}
function noemail_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
noemail_form_user_account_form_alter($form, $form_state, $form_id);
}
function noemail_form_user_account_form_alter(&$form, &$form_state, $form_id) {
if (is_array($form['#validate'])) {
$key = array_search( 'user_account_form_validate', $form['#validate'], TRUE );
if ( $key !== FALSE ) {
$form['#validate'][$key] = 'noemail_account_form_validate';
}
}
$form['account']['mail']['#required'] = FALSE;
}
function noemail_account_form_validate($form, &$form_state) {
$form['account']['mail']['#needs_validation'] = false;
}
and the install file in case it's key
<?php
function sharedemail_install() {
db_query("UPDATE {system} SET weight = -99 WHERE name = 'noemail'");
}
Hope this helps, wow this is a really old thread. Oh and note this is for d7
You can't really change another validation hook.
What you could try to do is build your own validation and on that form's validation (['#validate']) array only instantiate your own validation hook.
There is a optional mail module that makes the email field optional in user registration. Visit https://www.drupal.org/project/optional_mail ,
I hope it helps.
You could override the #required setting on the field using the Form API inside of a custom module with a hook_form_alter implementation.

Drupal: run custom code when a form is submitted

how can I run specific code when a form is submitted in Drupal ?
I'm using hook_form_alter to edit the form, but I actually need to collect the data inserted by user and run code when the user click on "Save / Register" etc
thanks
You can add callbacks to the submit array. It goes something like this:
function myform_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'some_form') {
$form['#submit'][] = 'mycallback';
}
}
function mycallback(&$form, &$form_state) {
// do stuff
}
Try adding the following function:
function myform_form_submit($form_id, $form_values){
print_r($form_values);
// custom code
}
worked for me. Hope it helps :)

Resources