3

How can I embed a view inside a user edit form depending on that user's role. I have used the Context module but it does not have the right conditions.

At the moment is uses the (user page -> User account form) condition. I cannot use the User role condition because it is for the currently logged in user.

6 Answers 6

5

You can use hook_form_alter and views_embed_view to alter the form and add the markup for a view.

5
  • I am trying to utilise drupal as much as possible so im avoid code for now. Thanks!
    – N dube
    Commented Jan 31, 2012 at 17:16
  • 2
    @Ndube Friendly advise: Don't use a hammer like a screwdriver.
    – googletorp
    Commented Jan 31, 2012 at 21:39
  • haha thanks, just testing the capabilities of drupal
    – N dube
    Commented Feb 1, 2012 at 9:10
  • @Ndube Drupal is more than modules you download, it's also a framework. Sometimes things are a lot easier to do with code. One of the strengths of Drupal is it's flexibility and how you can virtually anything with your custom code, without changing Drupal itself or the modules you use.
    – googletorp
    Commented Feb 1, 2012 at 9:17
  • googletorp could you write some code snippet to illustrate how to do it ??
    – oll
    Commented May 22, 2013 at 9:47
1

Use https://www.drupal.org/project/embeddedviewfield

EmbeddedViewField module can render a view regardless of whether the user is editing or displaying the entity that has the EmbeddedViewField.

1

I finally found an answer to this problem. Its called the context module.

  1. Create a new context and define some conditions. For me the condition was a certain role and the User Page (User account form)
  2. Define Reactions -> Add Block ( The Views Block in Question)

enter image description here

A small problem i have that i do not know exactly how to place my views in between specific fields.

I figured out, how to write hook_form_alter with views_embed_view:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    $form['my views block'] = array(
      '#type' => 'markup',
      '#markup' =>views_embed_view('myview','my_block_display'),
      '#weight' => 1,
    );
  }    
}
0

You can try with Panels module. Create a panel page with 2 areas: one for the form and one for the view. The view can be embedded if a block is provided by it.

1
  • Hey I've also tried the Panels way but I cannot load the right context meaning if the user being edited is a panel member.
    – N dube
    Commented Jan 31, 2012 at 17:15
0

This is done through user_profile_form_alter. To add to oll's answer to position the field on the profile form - the weight setting positions your field. If you are using vertical tabs/grouping to format you'll also need to put the field in the right group.

$form['my views block'] = array(
    '#type' => 'markup',
    '#markup' =>views_embed_view('myview','my_block_display'),
    '#weight' => 1,
  );
      $form['#group_children']['my views block'] = 'group_myverticaltab';
0

Please refer this link: https://www.drupal.org/node/1655012

Or try something like:

$viewName = 'view_name_goes_here';
print views_embed_view($viewName);

-hope it may help. Thank You.

2
  • 1
    Hello and welcome to Drupal Answers. You can improve your answer by providing more information on how to actually deal with the issue in question.
    – Wtower
    Commented Nov 11, 2014 at 10:52
  • Thank you for your comment and help. I am a newbie to drupal(started two weeks ago). Currently I am working with an entity type in which I have few forms-one of that requires a grid list of users with their images. My search keywords lead me here because I am facing an issue like this(just a user list form instead of user edit form-that I have). I hope I would solve this issue in a mean time and surely improve this answer once if I get it. Thank You. Commented Nov 11, 2014 at 11:44

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.