0

Right now, I want to create a form which only have an email textfield and a submit button.enter image description here

This is what I did in my "custom_request_price.module":

function custom_request_price_form(&$form, $form_state) {

  $form['email'] = array(
    '#type' => 'textfield',
    '#size' => '20',
    '#maxlength' => '255',
    '#required' => TRUE,
  );
  $form['email']['#attributes']['placeholder'] = 'Email Address';

  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Send');
  );
}

However, I don't know how to embed it to my article. I don't want to add this into a menu or make this as a new page. What I want to do is: when I open my article page, it directly showed up and visitors can enter their email address and simply to click "send" button.

My search: For doing this, I thought I need to add "drupal_get_form()" in my node-article.tpl.php to show this form on my article page. I am totally new for doing this. I don't know how to add form-ID in my custom module. Also, I don't know whether this is the correct way to embed my form.

1

The way I would go about it is to create the form within a Drupal block. In this manner, you could configure when & where the form could be placed on the page and with what types of content. You also wouldn't have to edit your theme if you'd ever wanted to add the form to another type of content.

Look at hook_block_info() & hook_block_view(), and reference the user module's implementation (user_block_info() & user_block_view()) for how it create its user login block.

| improve this answer | |
  • Cause I would like to store the visitor data and email them the content in my article(I thought I can add some token in the confirm email.) Therefore, I thought block might be not enough for me to do this. Do you have any idea about this? – jmu Jun 17 '16 at 21:48
  • There many ways to get the current node being viewed (e.g. menu_get_object()). – Shawn Conn Jun 17 '16 at 21:54
  • Thank you for your hint. And one more question, what if I want to show "thank you for your requirement"(text) instead of my form, after the visitors clicking the send button. I mean on the exact same position, hide the form& show the message. – jmu Jun 18 '16 at 21:26
  • You can build logic into hook_block_view() that can render different output depending on various states within your site. It will depend on how you want to track submissions (e.g. check for HTTP GET/POST requests, user session data, cookies, etc.). – Shawn Conn Jun 19 '16 at 22:29
  • I just want to check if the visitor click the button. If they click the button, the message will replace the form. Could you give me more info? I searched a lot but still have no idea. – jmu Jun 21 '16 at 21:05

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.