0

I have form with an alt attribute like the following.

<input type="text" alt="abc">

I want to replace that attribute with data-alt.

I tried the following code, but it doesn't seem to work.

function theme_form_alter(&$form, &$form_state, $form_id) {
    foreach($form["submitted"] as $newForm)
    {
        foreach($newForm as $keyid=>$frmArr)
        {
            if(isset($frmArr[]["#attributes"]["alt"]))
            {
                    $alt = $frmArr["#attributes"]["alt"];
                    unset($newForm[$keyid]["#attributes"]["alt"]);
                    $newForm[$keyid]["#attributes"]["data-alt"] = $alt;
            }
        }
    }
}

The following code is not working either.

foreach($form["submitted"] as $newForm)
    {

        foreach($newForm as $keyid=>$frmArr)
        {
            if($keyid=='#attributes'){
                $newForm[$keyid]['data-alt']=$newForm[$keyid]['alt'] ;
                 unset($newForm[$keyid]['alt'] );
                $form["submitted"]['#attributes']['alt'] = array("sss");
                form_set_value($form["submitted"]['#attributes']['alt'], array("aaa"), $form_state);
            }
        }


    }

What code should I use to achieve what I want?

4
  • 1
    isset($frmArr[]["#attributes"]["alt"]) This could never work. Isset is a check on whether something exists and [] is designed to create. Maybe all you need to do is delete those 2 characters. Commented Jan 18, 2017 at 9:47
  • i updated code that seems not working
    – gaurav
    Commented Jan 18, 2017 at 9:54
  • There's numerous things wrong with the rest of the code also actually. hook_form_alter is used to alter $form and/or $form_state. What you are doing is using them to create new variables, and then not using those to manipulate $form or $form_state. Besides that you should fix your coding style a bit. Read drupal.org/docs/develop/standards/coding-standards that and apply as much as you can. Commented Jan 18, 2017 at 9:57
  • theme_form_alter will never be called. It has to be the name of your module or theme.
    – Kevin
    Commented Jan 18, 2017 at 13:00

1 Answer 1

0
function act_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'the_id_of_your_form') {
    unset($form['submitted']['#attributes']['alt']);
    $form['submitted']['#attributes']['data-alt'] = 'abc';
  }
}

Won't this work?

2
  • this also not works
    – gaurav
    Commented Jan 18, 2017 at 10:04
  • Any idea why? Did you alter the actual id of your form in the code sample and does the code get executed or does nothing happen or what? Commented Jan 18, 2017 at 10:16

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.