Search code examples
drupaldrupal-6drupal-blocks

Creating "ON/OFF News Links" functionality for a block created with View Module


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!!


Solution

  • 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().