Jump to content

Recommended Posts

I basically want to do something seemingly pretty simple.

When a "user" (not admin but user) goes to the homepage of the site the module goes and get's a list of everything in the "node_type" table.  Basically it uses that to generate a multi-select box with all of the possible

node_types (content types) that are in the system.  Basically the select box is for them to select there favorites.  They select there favorites and it get's saved into a table called "selected_content_types" which stores the user

id and the content type. It basically looks through all of the ones they selected and adds it to the table. THat way

I can easily generate a list of there favorite content types later.  Also before it saves it clears anything already in the table for that user so they get a new list of favorites.

 

All of that is done and completely working.  I have that code below. I also have he install file which does install/uninstall and it all works.  The following code completely works as expected.

<?php
// $Id$
/**
* Implementation of the hook_help().
*/
function selectctype_help($path, $arg) {
  $output = '';  //declare your output variable
  // Setup help information for this module
  switch ($path) {
    case "admin/help#selectctype":
      $output = '<p>'.  t("Allows use to choose one or more content types.  Those content types will then be the only
  options available when they select a content type.  They also have the option of editing there favorites after they
  are chosen.") .'</p>';
      break;
  }
  return $output;
}
/**
* Implementation of hook_block();
*/
function selectctype_block($op = 'list', $delta = 0, $edit = array()) {
  // This is the admin list.  This allows them to setup the block in the admin panel
  if ($op == 'list') {
    $block[0] = array('info' => t('Content Type Selection Block ...'));
    return $block;
  }
  else if ($op == 'view') {
    // This is what shows the form on the add/content page.
    $block = array('subject' => t('Content Type Selector'),
      'content' => drupal_get_form('selectctype_typelistform'));
return $block;
  }
}
function selectctype_typelistform() {
  // Build form using select list from database
  $form = array();
  $result = db_query('SELECT * FROM node_type');  
  $list = array('Choose'=>'Choose');
  while ($row = db_fetch_object($result)) {
$list[$row->type]   = $row->type;
  }
  $form['select_list'] = array(
    '#type' => 'select',
    '#options' => $list,
'#multiple' => true,
    '#title' => t('Please select your favorite content types.'),
  );	
  $form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Save Settings'),
'#value' => t('Save Settings'),
  );
  return $form;
}
/**
* Implementation of Validate hook.
*/
function selectctype_typelistform_validate($form, &$form_state) {
  // If "Choose" was selected they can't submit.
  if (isset($form_state['values']['select_list']['Choose'])) {
    form_set_error('', t('You must choose an option or a series of options before submitting.'));
  }
  // If they selected nothing then they can't submit
  if (empty($form_state['values']['select_list'])) {
    form_set_error('', t('No option(s) were selected.'));
  }
}
/**
* Implementation of submit hook.
*/
function selectctype_typelistform_submit($form, &$form_state) {
  // Activate user global.
  global $user;
  // Clear out the content types selected by that user previously (if any).
  db_query("DELETE FROM {selected_content_types} WHERE user_id = %d", $user->uid);
  // Loop through options and add them into Database.
  foreach($form['select_list']['#post']['select_list'] as $k=>$v) {
db_query("INSERT INTO {selected_content_types} (user_id, content_type) VALUES (%d, '%s')", $user->uid, $v);
  } 
  // Form was ok.
  drupal_set_message(t('Your form has been saved.'));
}
?>

Ok so there we are.  Now here is my problem. When they select there list of favorites I want to then make it to where those are the only content types they can see.  So basically only there favorite content types will be viewable and accessible to the. Pretty simple in theory I thought. but not easy to implement. I have treid everything I can think of to make this work but nothing is working.  I don't want to make this dependent on another module. I want to make it work on it's on and so far it was going great until I tried to get this part working. I just need now is when they save the favorites it filters out somehow and only shows those node types then if they change there favorites again sometime ti just re-filters. I need it to be persistent even when they come back to the site that's why I also went ahead and databased it.

 

I have tried messing with the perm and access hooks, and node hooks and multiple other ways but nothing is working.

Can someone please point me in the right direction.  Google was not my friend here either I have spent two days involved in extensive searches and nothing has gotten me closer to this goal.

Link to comment
https://forums.phpfreaks.com/topic/152283-solved-drupal-module-creation/
Share on other sites

I thought I had a solution just now but it didn't work. I was looking up the hook_nodeapi.  I read all about it and it sounded promising but I set it up and print_r'd everything in it I was able to see them and then I was even able to do something based on type (like story type, or blog type). But I could not stop them from showing. I tried returning false when the node was story and I also tried unsetting node when it was of that type but neither of those worked either.

I am still looking and doing research but if anyone has any advice it would be greatly appreciated. I am using Drupal 6 by the way.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.