views:

63

answers:

3

Hi friends,

I'm a drupal newbie who needs some advice...

I have a news list block at homepage, created with View Module. It is listing all added news' title and link. Everything is cool so far. Now I need to add an ON/OFF option at admin side for homepage news block. When the setting is ON, it will work as it is. When it is OFF, only the titles will be listed with no linking to news detail page.

So, now where should I add this ON/OFF option? I have only add/edit/delete pages for each news, there is no common news page to add such option. Should I create an admin page with such ON/OFF option in? Also I think I need to create a db table to keep this ON/OFF status. and controlling this value at homepage block, if it is 1 or 0, and displaying links according to db value :/

it looks too long way

  1. Create db table
  2. Create an page with ON/OFF option in
  3. add php codes to update db for admin's choice
  4. get the db value in homepage block to display links, etc.

is there any shorter, better way to do what I need?

Appreciate helps so much!!! Thanks a lot!!

+1  A: 

create a form at admin/settings/on-off-switch. on the form submit function, variable_set('on/off switch', $value) (try using booleans for the value). then on the view theme, check for variable_get('on/off switch', $default_value) before printing the links.

barraponto
thanks for reply. I meant when I wrote that I'm a newbie :) You say about creating form at admin/settings/on-off-switch. creating that with any module like CCK, or anything else? can you please be a little more in details as here is a drupal dummy :/ appreciate your time giving this!!!
artmania
+3  A: 

You definitely don't need to create any database tables for something like that. If you want a basic admin page, you will need to write a simple module though. First follow this quick start guide for setting up a basic module. (Note: You don't need to add those database queries in your .install file)

Once you have your module enabled...

1) In your mynewmodule.module file, add a menu entry to tell Drupal where your admin page can be accessed:


function mynewmodule_menu() {
  return array(
    'admin/settings/mynewmodule' => array(
      'title' => 'My New Module',
      'description' => 'Change settings for news display.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('mynewmodule_admin_form'),
      'acces callback' => 'user_access',
      'access arguments' => array('administer site configuration'),
    ),
  );
}

2) Also in your mynewmodule.module file, add a function to create the form you just referenced in the menu entry:


function mynewmodule_admin_form() {
  $form = array();
  $form['mynewmodule-on-off-switch'] = array(
   '#type' => 'checkbox',
   '#title' => t('Enable news links'),
   '#description' => t('Control whether news items are linked to stories'),
   '#default_value' => variable_get('mynewmodule-on-off-switch', 1),
  );
  return system_settings_form($form);
}

3) Clear your cache to make Drupal recognize your admin page (you need to clear any time you make changes to mynewmodule_menu()). You can clear it here: admin/settings/performance

4) Visit admin/settings/mynewmodule to see your admin form. The way it works is when you save the configuration, Drupal will save a variable called "mynewmodule-on-off-switch" (same name as the array key in the form) to the variables table in the database. You can get this value anywhere by using variable_get().

wynz
wowowo! man! you just saved my life!! it works perfectly! appreciate so much for your clear-detailed post! I would love to buy you a cold beer, where about are you ? seriously :)
artmania
I also used the http://drupal.org/project/views_customfield module to add my php conditions :) now working great :) only 1 problem left. I can't see the module permission settings at permissions page :/ I need to give access to admin user for client :/ how can I control the permission settings for the custom module?
artmania
Glad to help. If you find yourself in Thailand I'll take you up on the offer. To set up custom permissions you would use hook_perm() by adding a mynewmodule_perm() function to your module and defining a new permission. Then in mynewmodule_menu() you would use that permission as the access callback.
wynz
Thailand, hmm i need to arrange a holiday there :) so I can meet you for beer :) ps, I googled the details for hook_perm(). Now I can see the custom module at permission setting page. I enabled that, but I still have no access to that page with a role (that I have given permission actually) :/
artmania
Make sure you've cleared the cache after making changes to the menu settings.
wynz
I have cleared cache. I can access the related admin page with the super-user (first account), but not with the site-admin account which I have created for client and give access for creating news, editing page content etc. :/
artmania
And you changed the access callback in your menu setting to be "array('your permission name')"? Only other thing I can think of is trying a different path instead of admin/settings in case there's something special about that one.
wynz
just added the code above (to the question above). I jsut can't make it work. I looked in each google result :/
artmania
Oops, I was mixing up 'access arguments' with 'access callback'. I revised the example to show how it should be set up. You would only need to change 'access arguments' to use the permission you made. Hopefully that works for you.
wynz
+1  A: 

Drupal's weakness, IMHO, is the sheer number of admin settings to configure to get a site up, and you don't want to be adding to that.

What I would do is to have the view expose two different blocks, one with the full view, one with the abbreviated view. Then all the configuration can be done through the block interface, which will be much more flexible in the long run. By, for example: using wildcards or php code for block visibility; showing different views to users with different roles; allowing visitors to control which view they see; exposing the two views to the theming engine more cleanly; and integration with any other module that works with blocks.

CurtainDog
I'm hazy on artmania's goal, but this might be the best option. It would also improve caching. I'd be more interested in keeping yet another menu item out of the admin drop-down.
Grayside

related questions