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

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
Create db table
Create an page with ON/OFF option in
add php codes to update db for admin's choice
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!!

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

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.

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.

Related

Mixing Wordpress 3.7.1 Page Templates and Custom Post Types

Overview
I have a Custom Post Type called Locations. When the user visits /locations, I would like them to see some general information about all the locations. When the user visits /location/{a-location}, I would like them to see specific information about "a-location". Sounds easy enough.
1st Solution Attempt
I created a Locations page in the Wordpress dashboard to hold the content for the /locations url. I then created an archive-locations.php file and a single-location.php. archive-locations.php pulls info out of the Locations page and it lists all of the custom post type Locations. When the user clicks on a link for a location, the user gets redirected to /locations/{a-location} and single-location.php gets called. This almost does the trick, but it just doesn't feel right and it's creating some other problems.
The Problem
When a user visits /locations I would prefer to be working with a page template for the Locations page (instead of the archive-locations.php page). I'm using the Yoast plugin to specify a custom meta title and description for each page. Since archive-locations.php is being used, all of this meta gets ignored.
2nd Solution Attempt
So I created a page-locations.php template for the Locations page and I was hoping that it would get called instead of the archive-locations.php. Unfortunately, it does not. I then tried removing the archive-locations.php thinking that maybe it was taking precedence. That didn't help either; Wordpress simply renders index.php. If I disable the Custom Post type and visit /locations, then my page template is called correctly. It looks like naming a custom post type the same name as a page causes Wordpress some issues. However, I need to be somewhat similar for the URL structures to work out. /locations needs to pull from a page template; /locations/{a-location} needs to pull from another php pfile.
Question
Is there any way to get page templates and custom post types with similar slugs to work together? If not, I guess my only option is to enhance my header.php and be smarter about determining the title and description when I'm on /locations.
Here's the code that registers my custom post type in case I'm doing something wrong:
$args = array(
'labels' => $labels,
'description' => 'Holds our location specific data',
'public' => true,
'menu_position' => 20,
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes' ),
'hierarchical' => true,
'has_archive' => true,
'rewrite' => array('with_front' => false, 'slug' => 'locations') //remove /news/ from the permalink when displaying a custom post type
);
register_post_type( 'locations', $args );
(I also tried changing has_archive to false, but it didn't help.)
What you are trying to do, I once managed to get done easily. I mean having custom post type and use its slug with cusotm page.
Did you try to flush rewrite rules? (just visit Permalink page and save the rules as they are)
So I created a page-locations.php template for the Locations page and I was hoping that it would get called instead of the archive-locations.php. Unfortunately, it does not. I then tried removing the archive-locations.php thinking that maybe it was taking precedence. That didn't help either; Wordpress simply renders index.php
You do not write, how do you load your page template. Do you rely on automatic WP connection of pageslug and page-pageslug.php or you set page template through Page attributes menu?

Adding custom taxonomy to media gallery in wordpress admin

I have added a custom taxonomy to Media, which is showing up as a text field in the Media admin section. I would like this to be the typical checkbox format as it exists in the custom post type admin page. Is there a way to override this in the functions to make this custom taxonomy show in checkboxes, so the user could easily choose which image belongs to a specific taxonomy entry?
Here is the code I used to bring the taxonomy into the Media Gallery:
register_taxonomy('Categories',array('project', 'slides', 'attachment'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'categories' ),
));
In the first line, by adding 'attachment' to the array, it added the Project Categories field in the Media Gallery. Now I just need to make this a list of checkboxes containing the current taxonomy entries. Any thoughts on how to achieve this?
I found this article, but having never used filters, it was a bit perplexing as to how to make this work for me:
https://wordpress.stackexchange.com/questions/29858/adding-category-tag-taxonomy-support-to-images-media
You are most of the way there. To render a taxonomy category as a special HTML display, like a list of checkboxes, the best method is to use the built-in WordPress Walker class. It is made exactly for this sort of thing.
http://codex.wordpress.org/Function_Reference/Walker_Class
I use this very method to create a new "SLP_Tagalong" walker class that renders a list of my taxonomy categories as a list of checkboxes (I only show text names but it could easily show the marker images) whenever anyone edits a store location.
I have the modified Walker Class I can share if you would like to see it. I'd post here but it is 150 lines. Send me a PM and I'll shoot it back that way.
I am sure the walker class would have worked successfully, but looking at the codex reminded me of string theory and existentialism. The upside is with WP 3.5.1, when you associate a taxonomy to 'attachment' set to Hierarchal, the checkbox appears in the Media Library by default.
YAY!!
This may not answer the question posed thoroughly though so I will leave it open for anyone who wants to stab at this.

Anyone know how to add a field or column to the /ADMIN/CONTENT listing page? DRUPAL 7

I would like to add a field / column to the Content Administration Overview page but it appears the easiest theme override to do this has been deprecated with D7.
In D6 I could just override the method:
theme_node_admin_nodes($form)
But this method no longer exists for D7. What's the equivalent replacement or do I actually need to hook into node_admin_nodes() now and modify the form directly?
For me it was super easy with these two modules:
views bulk operations (VBO)
administration views (needs VBO)
As soon as both modules are installed and activated you can go to your views (admin/structure/views) where now 3 additional views appear (Administration comments, Administration nodes, Administration users). You then just need to edit the view "Administration nodes", where you can add and arrange everything you want as usually with views.
I wanted to add a column displaying all content's nids. Worked super well!
You'll have to hook into the form, the theme element has been completely removed node_admin_nodes() in Drupal 7.
It's actually node_admin_content() that you'll need to hook into as node_admin_nodes() is no longer a form function, it just builds up elements that are used by node_admin_content().
Fortunately the elements in node_admin_nodes() and node_filter_form() (the two functions used in node_admin_content() to build up the page) are nicely structured and will be very easy to override.
I've been able to add an element to the bottom of the table. Although I am unsure how you ADD a coloumn into the body of the table?
function seven_form_alter(&$form, &$form_state, $form_id) {
drupal_set_message("Form ID is : " . $form_id);
//get node_admin_content
//$nodeAdmin = drupal_get_form("node_admin_content");
// Add a checkbox to registration form about agreeing to terms of use.
$form['node_admin_content']['poland'] = array(
'#type' => 'checkbox',
'#title' => t("I agree with the website's terms and conditions."),
'#required' => TRUE,
);
}
The Administration Views module replaces a lot of admin listings with real views (as in Views module) that you can edit and configure any way you want it.

Administration menu

I'm looking for some beginners resources for drupal. I've been writing my own module as a way of teaching myself the basics of development as I like the idea of having Drupal to go to when Wordpress can't manage. (Although wordpress is fast becoming as accomplished as Drupal, but that's not what the questions about.)
I have the module showing up in the enable/disable screen and the module.install file works a treat. What I'm stuck on is generating an admin area to edit entries in the table I'm using. I have a module_admin_settings() function in module.admin.inc, plus module.menu () in module.module.
But it isn't appearing in the menus and I'm stumped as to why that is. So I'm hoping someone knows a good tutorial that explains how to generate admin options.
If you want to become serious with Drupal development, I would advice you to go through the Pro Drupal developemnt book. It has been the single most helpful Drupal book I have ever read.
As for your question, to create a admin section is not much different from creating a view some where. What you need to do is.
Implement hook_menu to register the url you want to use, in your case admin/settings/name should be fine
Implement a call back function that should be called when a user go to the your. In your case you don't need to implement one as you should use drupal_get_form, but instead you need to create the form that the user should see using the form API.
You need to place this info in your hook_menu like this:
function modulename_menu() {
$items = array();
$items['admin/settings/modulename'] = array(
'title' => 'Menu item',
'description' => 'The description of the menu item. It is used as title attribute and on the administration overview page.',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_id'),
'access arguments' => array('administer modulename'), // access restriction to admins only
);
return $items;
}
Then you need to implement validation and submit handlers for your form, is you use a system_settings_form, you can get a lot of the work done for you.
Whenever you make changes to hook_menu, you need to clear the (menu) cache, as Drupal caches this information to optimize performance.
That's it. Now you don't need to use different files in your module. If the module is small all could just go into the .module file. But for bigger modules, you can place some of your code in .inc files to organize your code a bit. Fx modulename.admin.inc for your admin callbacks/functions, modulename.page.inc for your regular callback/functions etc.
http://drupal.org/node/206761 is what I use as a starting point.

Create dynamic link in drupal

Could anyone tell me how to create a dynamic link in drupal?
I want to create a link to a group membership list. I have created the view with the argument. How do I create the menu item link to the view? It will be different depending on the group.
Thanks
I create a redirect link for such situations. For example, if I want a menu link to direct the user to their user edit page, I will implement hook_menu() as follows:
function my_module_menu()
{
$menu['user/cp'] = array
(
'title' => 'User Control Panel',
'page callback' => 'user_cp_redirect_page',
'access callback' => 'user_is_logged_in',
'type' => MENU_CALLBACK,
);
return $menu;
}
Then, I set the page callback to look like this:
function user_cp_redirect_page()
{
global $user;
drupal_goto('user/' . $user->uid . '/edit');
}
In short, you can't. Drupal's menu system caches the entire menu: menu items can't appear differently for different users (although you can set access permissions for them). If you want menu items to appear, you have to either register them in hook_menu() (which is only called when the menu is rebuilt), or you have to add them manually in Menu administration.
The handbook page on the Menu system provides more information about this.
However, you can create a fake menu using a block display in your view and the HTML list row style. This is useful if you wanted to have a category listing block, outside the main menu structure.
It sounds like you may need to use views arguments to filter your results if you are pulling different "groups" based on a single view. I would start here to learn more about views arguments: http://drupal.org/node/54455, if this is indeed what you are trying to accomplish.
EDIT:
I guess it would help if I read the question fully. It sounds like you are already using arguments for your view. In this case you already have created dynamic links to your page. Try putting the arguments for each "group" at the end of the URL. For example if created a page display with a path news, you can pass the argument by appending it at the end of the URL. So, if you created an argument using "Node:Created Year" you could then filter this content by putting something like example.com/news/2009 to access only the nodes that were created in 2009. What you pass is dependent on the type of argument you have created.

Resources