7

I have a View with some fields and a couple of link items.

What I want to do is when I click on the link items, a node creation form associated with link should appear inside the view (the AJAX way).

The solution that I can think of is to embed the forms inside the view, but have no idea how to do that?

What is the best way to implement this?

5

Hope this will help you,

In Drupal 7 you can achieve by the following steps

  • You can embed a node creation form inside a VIEW

    • Either in VIEW Header or you can created a new VIEW and attach it to your VIEW
  • For this example I am embedding inside Views Header. Embedding inside Views Header will be the better options. Click to Add Views Header

    enter image description here

  • Select Global: Text area

    enter image description here

  • Add the PHP code and don't forget to change the Text Format to PHP

    enter image description here

  • PHP code

    <?php 
        module_load_include('inc', 'node', 'node.pages');
        $node_form = node_add('node_type');
        print drupal_render($node_form);
    ?>
    

Note :

  • Don't forget to replace node_type in the PHP code.

  • Use hook_form_alter for node form redirection, as after the node creation the form will redirect to node detail page. If you want to redirect to your page then use $form_state['redirect'] = 'mycustompage'; reference

  • Enable PHP Filter module using admin/modules

enter image description here

  • Set the Permissions for this module by enabling "Administrator" for Execute PHP Code as follows:

enter image description here

If any one has better solution, please share it.

  • Would you please expand on "Use hook_form_alter for node form redirection"?? – KyleM Nov 6 '13 at 22:32
  • Also, if you could include that users have to enable the "PHP Filter" module, and select "Administrator" under the permissions for "Execute PHP code". Thank you for your solution! – KyleM Nov 6 '13 at 23:25
  • 1
    Thanks for your suggestions I appreciate that. I missed out few comments. I have updated my answer. – inizio Nov 7 '13 at 6:16
  • Your welcome enjoy :) – inizio Nov 28 '13 at 6:16
  • This solution will render without passing certain routines and also implements a security issue. For example the simplify module won't work to remove elements from the form and you are not able to "build" forms from this content type the "views" fields way. You rather simply implement the node form "as is" from core, which is in most cases useless, since this form is too cluttered to get implemented in a block (sidebar etc...). Adding php code from the admin UI should be avoided on bigger projects with security concerns. – nilsun Jun 4 '14 at 1:19
1

If you are happy to play with some custom code, a similar question was asked on StackOverflow, and there were some pretty extensive solutions to embed forms within a view. All you would have to do is use the form_id of the node creation form specific to your type, and place it within one of the solutions posted here within your site.

1

Have a look at Inline Entity Form for this; the Commerce Kickstart employs this as part of the recipe for managing products and product variations. I'm sure there's more to it than I can recall right now, but if this looks promising I'll circle back and improve this answer (or, perhaps others will help improve this answer).

A potential alternative (or complimentary?) module might be Entity Views Attach, which, I believe is also employed in commerce kickstart.

  • Not sure if this emphases the wanted approach from the OT/OP. IEF and EVA are more targeted for survey forms approaches than for publishing content forms. – nilsun Jun 3 '14 at 23:45
0

I can't comment the Views Headers solution posted by inizio, I haven't enough reputation.

Well I want to post that you can solve the form redirection after submission with Rules, and you don't have to create a custom module the hook_form_alter.

  • Create a Rule triggered on "After saving new content" for your node type
  • Add condition, compare data and compare the selector [site:current-page:path] is equals with your view path
  • Add action page redirect to the view path

Can be useful too Form Block and Advanced Form Block modules, but putting php code in view headers you prevent to install extra module

And is done !

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.