2

Let's say I want to make a bunch of nodes based on what's in a specific field when someone clicks a button on their user profile.

Is this possible with a custom built module? And if so what hook would I use to display that button on the user profile, and how would I get it to call a custom function on click?

2 Answers 2

1

Yes a custom module could accomplish this. I'd use drupal's form api to create a simple form and output it to a block that you display on user profile pages. The code below should get you started.

Create the form. The hidden element is grabbing the users id from the url.

function my_module_form($form, &$form_state) {
  $form = array();

  $form['uid'] = array(
    '#type' => 'hidden', 
    '#value' => arg(1),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );

  return $form;
}

function my_module_form_submit($form, &$form_state) {
  $uid = $form_state['values']['uid'];
  // do some stuff
}

Now use hook_block_info and hook_block_view to create a new block containing your form.

/**
 * Implementation of hook_block_info().
 */
function my_module_block_info() {
  $blocks['my_module'] = array(
    'info' => t('My Name'),
  );
  return $blocks;
}

/**
 * Implementation of hook_block_view().
 */
function my_module_block_view($delta='') {
  $block = array();
  switch ($delta) {
    case 'my_module':
      $block['subject'] = t('My Title');
      $my_form = drupal_get_form('my_module_form');
      $block['content'] = drupal_render($my_form);

      break;
  }
  return $block;
}

Here's an article on creating a module in D7 if you are not familiar with how to do that. http://www.justinphelan.com/post/how-write-your-own-module-drupal-7

0

Wow, this is a question with many solutions. Some stuff I can quickly think of:

Developers way:

  1. create a custom module: https://www.drupal.org/developing/modules/7
  2. implement hook_menu to create callbacks for your links
  3. implement your logic in the callback functions defined for your entries in hook_menu.
  4. "presentation layer". Depending on the structure of your website and your profiles:

    a) Create a block with your links in it, references: block_example, link function

    b) Implement a "pseudo field" on fieldable entities (e.g. user, profile2)

    c) Use link function in your profile template file e.g. user-profile.tpl.php

extra points might be awarded if you use the link function and your hook menu implementation together with the ajax framework. example.

Site builders way (one way.. there are many):

  1. create a flag
  2. create a rule for flagging items
  3. in that rule implement your logic
  4. optional: undo the flagging (so the user can do this action again)

All of the mentioned solutions will probably require some logic for handling the access and display of that button as well.

(one) developer way, if this button appears on user/id:

<?php
global $user;    
if (arg(0) == 'user' && arg(1) == $user->uid && null === arg(2) {
  print l('do something', 'yourcallballback/' . arg(1));
}

site builders way (for the example from above):

usually there's no additional access check required here, you can setup that flag to be global and only clickable once if you want to make sure that it's only available once. for the display part you probably really have to use code to only show that flag if the current user visits his own profile.

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.