0

I created module_templat with code:

<?php

 function module_template_menu(){
    $items = array();
    $items['them-noi-dung'] = array(
        'title' => t('Khai báo hóa chất'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('module_template_select_form',2),
        'access callback' => true,
    );
    return $items;
    }

function module_template_select_form($form, &$form_state){
    $form = array();
    $form['select'] = array(
        '#type' => 'radios',
        '#title' =>'Lựa chọn hình thức khai báo',
        '#options' => array('0'=>t('Khai báo lần đầu'), '1'=>t('Đã từng khai báo')),
        '#default_value' => '0', 
    );
    $form['ma_so_thue'] = array(
    '#type' => 'textfield',
    '#size' => '20',
    '#title' => 'Vui lòng nhập vào Mã Số Thuế',
    '#states' => array(
        'visible' => array(':radio[name="select"]' => array('value'=>'1'),  
                    ),
            ),
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Tiếp tục'),
    );
    return $form;
    }

function module_template_select_form_submit($form, &$form_state) {
    $select = $form_state['values']['select'];
    $masothue = $form_state['values']['ma_so_thue'];
    if(isset($select)){
        $masothue = $form_state['values']['ma_so_thue'];
        $form_state['redirect'] = 'node/add/khai-bao-hoa-chat';
        }
    }


function module_template_form_alter( &$form, &$form_state, $form_id ){
    $masothue = $form_state['values']['ma_so_thue'];
    if ($form_id == 'khai_bao_hoa_chat_node_form') {
        $form['field_ten_to_chuc']['und'][0]['value']['#default_value']= $masothue;
        }
    }
function module_template_theme($existing, $type, $theme, $path) {
    $items = array();
    $items['khai_bao_hoa_chat_node_form'] = array(
        'render element' => 'form',
        'template' => 'khai-bao-hoa-chat',
        'path' => drupal_get_path('module', 'module_template'),
        );  
    return $items;
    }

Why I can not get value of $form_state['value']['ma_so_thue'] in module_template_form_alter(&$form, &$form_state, $form_id) when form submited?

2

The code you are showing is wrong for two reasons:

  • hook_form_alter() is invoked for every form Drupal is showing; this includes Drupal core forms, as well as forms implemented by third-party modules. It is not invoked for the forms of your own module. So, you first need to check for which form the hook is being invoked, then you can access the form fields the form of your module is using.

  • hook_form_alter() is not invoked when the form is submitted, but before the form is rendered. So you cannot use $masothue = $form_state['values']['ma_so_thue']; there. You need to add a new submission handler from hook_form_alter(), and in that submission handler you can get the submitted values, but then you cannot alter the default value for a form field in a submission handler.

  • Three reasons: t-function should only take English language. – leymannx Sep 16 '17 at 23:36
0

Because there is no $form_state['values']['ma_so_thue'] in hook_form_alter (even taught you have Ajax or #rebuild it in form validation or you are in edit mode),
if you are in edit mode in hook_form_alter you can get value from $form['ma_so_thue']. ( dpm it and see what you have there, it will be something like $form['ma_so_thue']['und']['0']['#value'] )

  • No. I want get value $form['ma_so_thue'] to module_template_form_alter() when submit module_template_select_form() – Lê Quang Tú Sep 7 '15 at 5:37
  • @LêQuangTú please tell me more, you want get ma_so_thue' value in submit of your form ? or in form_alter ? – Yusef Sep 7 '15 at 5:55
  • i have ajax with dropdownlist in a form alter. i want get value it after selected – Lê Quang Tú Sep 27 '17 at 4:09
0

In hook_form_alter you won't get $form_state['values']['ANYFIELD']. Try $form_state['build_info']['args'][0]['ANYFIELD'].

  • It doesn't get that either, since hook_form_alter() is not invoked when a form is submitted, but before the form is rendered. – kiamlaluno Jul 11 '16 at 11:57
0

textfield always use fieldname->und->0->value-> sequence in array so,try this.. $form['ma_so_thue']['und'][0]['value'];

  • This is wrong: $form['ma_so_thue'] is not an entity field, so you cannot use $form['ma_so_thue']['und'][0]['value']. Also the OP is asking for getting the submitted values, which are not in $form. – kiamlaluno Jul 11 '16 at 11:47
0

enter image description here

Your are accessing an object as an array.instead do this

$is_live = $form_state->getValue('settings')['is_live '];

it works for me in drupal 8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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