0

I am following https://www.drupal.org/docs/8/api/form-api/introduction-to-form-api for my hello world module.

Below is my code

namespace Drupal\hello_world\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements an hello_world form.
 */
class HelloWorldForm extends FormBase {

    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'hello_world_form';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {

        $form['phone_number'] = array(
            '#type' => 'tel',
            '#title' => $this->t('Your phone number'),
        );
        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => $this->t('Save'),
            '#button_type' => 'primary',
        );
        return $form;
    }

    function hello_world_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
        if ($form_id == 'hello_world_form') {
            $form['phone_number']['#description'] = t('Start with + and your country code.');
        }
    }


}

I also cleated cache. Not sure why form is not altering.

  • This is not the proper way to do it. Procedural alter hooks can only go in .module files and some (like hook form alter) can also go in .theme files. They cannot reside in class files. – Kevin Sep 21 '17 at 11:35
0

Use your hello_world_form_alter in your module file. I guess should be hello_world.module. I guess you already have a folder hello_world where you placed the file above. In this folder you should also have an .info.yml file at the same folder place your hello_world.module file.

Please check https://www.drupal.org/docs/8/creating-custom-modules. You will find a lot of tutorials about how to create a custom module. Just google it!

  • How to use in module file? I'm NewBie – Ankit Shah Sep 21 '17 at 8:02
  • @Ankit I updated my answer. Have a look. – Ismini Sep 21 '17 at 8:19
  • That I am following. I just need to know when I'm wrong or what needs to be done @Ismini – Ankit Shah Sep 21 '17 at 8:44
  • @Ankit At this tutorial there is also drupal.org/docs/8/creating-custom-modules/basic-structure which lets you know about the files used in a module. As I said earlier, you need to place your hook (hello_world_form_alter) in he hello_wrold.module file. In your class you built the form and you use the hook_form_alter to alter the form which built in your class. – Ismini Sep 21 '17 at 8:55
0

Remove function hello_world_form_alter from Form file & add below code in hello_world.module

use Drupal\Core\Form\FormStateInterface;


/**
 * Implements hello_world_form_alter().
 */
function hello_world_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  if ($form_id === 'hello_world_form') {
    $form['phone_number']['#description'] = t('Start with + and your country code. - Test');
  }
}

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.