Theming CCK node in Drupal 5 - drupal

I need to add a few links to the bottom of node (custom type) form in Drupal 5.
I've tried contemplate module, but it actually deals only with custom fields (contemplate template doesn't contain node title, description, taxonomy fields) - while I need to add links below or above actual body field.
How can I make this possible?

Here is the most useful recipe :
from(Theming CCK Input Form | drupal.org)
Example (to use in themename/template.php file):
<?php
function phptemplate_product_node_form($form) {
global $user;
$vars = array('user' => $user, 'form' => $form);
$vars['title'] = drupal_render($form['title']);
$vars['body'] = drupal_render($form['body_filter']);
// etc
return _phptemplate_callback('product_edit', $vars);
}
?>

Related

Silverstripe tumblr-like Post Types

I am trying to create a back-end interface for silverstripe that gives the CMS user the option to choose between a set of Post Types (like tumblr) in Silverstripe3. So they can choose to create a News Post, Video Post, Gallery Post, etc.
I initially started off giving all Posts the necessary fields for each Type and adding an enum field that allowed the user to choose the Post Type. I then used the forTemplate method to set the template dependent upon which Post Type was chosen.
class Post extends DataObject {
static $db = array(
'Title' => 'Varchar(255),
'Entry' => 'HTMLText',
'Type' => 'enum('Video, Photo, Gallery, Music')
);
static $many_many = array(
'Videos' => 'SiteVideo',
'Photos' => 'SitePhoto,
'Songs' => 'SiteMp3'
);
public function forTemplate() {
switch ($this->Type) {
case 'Video':
return $this->renderWith('VideoPost');
break;
case 'Photo':
return $this->renderWith('ImagePost');
break;
etc...
}
function getCMSFields($params=null) {
$fields = parent::getCMSFields($params);
...
$videosField = new GridField(
'Videos',
'Videos',
$this->Videos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
$photosField = new GridField(
'Photos',
'Photos',
$this->Photos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
return $fields;
}
}
I would rather the user be able to choose the Post Type in the backend and only the appropriate tabs show up. So if you choose Video, only the Video GridField tab would show up. If you choose Photo Type only the Photo's GridField would show.Then I would like to be able to call something like
public function PostList() {
Posts::get()
}
and be able to output all PostTypes sorted by date.
Does anyone know how this might be accomplished? Thanks.
Well the first part can be accomplished using javascript. Check out this tutorial and the docs let me know if you have questions on it.
The second part would be trickier but I think you could do something with the page controller. Include a method that outputs a different template based on the enum value but you would have to set links somewhere.
I managed this with DataObjectManager in 2.4.7 as I had numerous DataObjects and all were included in one page but I'm not sure if that is feasible in SS3.
return $this->renderWith(array('CustomTemplate'));
This line of code will output the page using a different template. You need to include it in a method and then call that method when the appropriate link is clicked.

Can I hide a Category (and its subcategories) from the sidebar?

I put the widget category on the primary sidebar, from the dashboard. Than, on code, I use :
<?php get_sidebar('left'); ?>
and it create the code for the categories. Now, I'd like to hide the category with tag_ID=6, and all of its subcategories.
How can I do it?
Tried this tutorial, but seems that I don't have the $cat_args = "orderby=name&show_count={$c}&hierarchical={$h}"; line? I'm on last version of WordPress, 3.4.2
The tutorial seems outdated, so I wouldn't rely on that. It is not necessary to hack around in the WordPress-source - create a simple Plugin which hooks into the right filters.
In your case those filters are widget_categories_dropdown_args (when you select "Display as dropdown" in the Widget-options) and widget_categories_args (if the Widgets displays the list as normal text with links).
With that knowledge you can now code the actual plugin (I've called it Myplugin, I think you should rename it) - just put that PHP code into the file wp-content/plugins/myplugin.php:
<?php
/**
* #package Myplugin
* #version 1.0
*/
/*
Plugin Name: Myplugin
Plugin URI: http://example.com
Description:
Author: You
Version: 1.0
Author URI: http://example.com
*/
// Create a list with the ID's of all children for
// the given category-id
function myplugin_recursive_filter($catid) {
$result = array($catid);
$cats = get_categories(array(
'child_of' => $catid,
));
foreach($cats as $category) {
$result[] = $category->cat_ID;
}
return implode(",", $result);
}
// Actual filter function. Just set the "exclude"
// entry to a comma separated list of category ID's
// to hide.
function myplugin_filter_categories_args($args) {
// 6 is the "tag_ID"
$args['exclude'] = myplugin_recursive_filter(6);
// or hard code the list like that:
//$args['exclude'] = '6,10,11,12';
// but you'd have to include the ID's of the
// children, because "eclude" is not recursive.
return $args;
}
// Register the filter to the relevant tags
add_filter('widget_categories_dropdown_args',
'myplugin_filter_categories_args', 10, 1);
add_filter('widget_categories_args',
'myplugin_filter_categories_args', 10, 1);
The function myplugin_recursive_filter is necessary, because the exclude-entry is not recursive (except if you check "Show hierarchy" in the widget options). If your categories don't change that much you could replace function call with a hard-coded list of ID's (with the children) for better performance.

Treat the node nids as a field (for display only in a content type) in drupal 7

I need to use the nid of a node as field in a content type: i need to choose where to print it (put it before some fields but after others) and format it as i wish. the only thing i could think about is create a "fake" custom field with no widget to insert it buth with a theme formatter to display it but it seems to me that this is a little to complicated. How should i do it?
If I understand correctly, you just want to expose data to the node view. Could it be as easy as using hook_node_view() from a module?
With that, you can set a 'fake' field to be sent out to the content array of the node, which you can access in the node template.
From drupal.org:
<?php
function hook_node_view($node, $view_mode, $langcode) {
$node->content['my_additional_field'] = array(
'#markup' => $additional_field,
'#weight' => 10,
'#theme' => 'mymodule_my_additional_field',
);
}
?>

Programmatic Views in Drupal 7

I'm trying to create two views.
View-1 is a list of nodes.
View-2 is an image gallery associated with each node.
I basically want to pass the node title from View-1 to a programmatic View-2, so that each row in View-1 will load View-2(with a result set filtered by the title of View-1!).
I'm confused about the approach. Should this happen in a custom module, preprocess functions, or some combination thereof?
I run into this a lot - wanting to pass an argument from a primary view to a secondary view that displays with each result.
I realize that the question is a bit general, but I'm curious how folks with more experience would approach this problem.
I've done this before on D6 where basically I just create a couple template tpl.php files for my View-1.
Inside my View-1 template for Display Output (views-view--default.tpl.php in D7 now)
I would simply programmatically find the value passed or returned by View-1 for this row.
In your case on each row you would check to see which node is returned by View-1 and then I'd add code in my View-1 template to programmatically load View-2 based on the current View-1 row (ie. node in your case.)
Make sense? 5 months late on the response but I was looking for a refresher and seeing if there's a better way to do this now in D7.
UPDATE:Just did this on my new D7 install. As an example I'll explain how it relates to my Ubercart implementation.
Ubercart, when installed, has it's main "home" shop page located at mysite.com/catalog
This page, when loaded, calls a View created by Ubercart called uc_catalog_terms. It is a taxonomy based view and all it does is grab all your Catalog taxonomy categories and render them.
E.g
As a clothing store, when you navigate to mysite.com/catalog, all you'll see at this page is a grid structure like:
Sweaters Shirts Jeans
My requirement was that I needed to show the shop catalog categories/terms on this page, but ALSO show 3 random products (images) from that category/term below it each catalog category.
E.g
Sweaters
Random Sweater #1 - Random Sweater #2 - Random Sweater #3
Jeans
Random Jean #1 - Random Jean #2 - Random Jean #3
How is this accomplished?
I created my own brand new custom view (no page or lock, just default) which grabs 3 random product images based on a taxonomy term ID argument and renders 3 linked product images. I'll call this custom view random_catalog_items. If 15 is the term ID for Sweaters, when this view is called with the argument 15 it will only render 3 random linked sweater product images.
I now went back to uc_catalog_terms view and created a views-view-fields--uc-catalog-terms.tpl.php (Row Style Output) template file.
THE DEFAULT VIEW VERSION OF THIS FILE (BEFORE MODIFICATION) IS:
<?php foreach ($fields as $id => $field): ?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<?php print $field->wrapper_prefix; ?>
<?php print $field->label_html; ?>
<?php print $field->content; ?>
<?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
THE MODIFIED VERSION BECOMES:
<?php foreach ($fields as $id => $field): ?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<?php print $field->wrapper_prefix; ?>
<?php print $field->label_html; ?>
<?php
$title = str_replace('/','-',strtolower($field->raw));
print '<img src="'.drupal_get_path('theme','my_theme').'/images/catalog/'.$title.'-header.png" />';
print '<hr style="width: 100%; background: #000; height: 2px; margin-top: 3px;"/>';
// get the taxonomy term ID
$tid = $row->tid;
// render the 3 random items
if ($random_products = views_get_view('random_catalog_items' )) {
print $random_products->execute_display('default', array($tid));
}
?>
<?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
So as you can see inside the first View, for every row that is rendered I get the current taxonomy term ID being shown through the available row result object - $row->tid and then I simply call my created view for each row, passing along this Term ID as the argument for it. I leave a lot of the default code in there but inside my view configurations the LABELS and such are set to HIDDEN so they don't even render anyway.
In your case it should be very easily adaptable to just pass a Node NID instead of a Taxonomy Term ID.
VOILA IT ALL WORKS! View within a View! Hope this helps :)
It helps to have the Devel module loaded since then inside these View templates you can debug and see what variables are available to you via something like print krumo($row).
Personally I would avoid views here alltogether.
A simple module using a hook_menu to define the menu-items and two simple menu-callback-functions dealing with the required parameters.
The alternative would be to make all the custom parameters and custom query-filtering and tables known to views.
My (pseronal) rule of thumb is:
If you are certain you will re-use the code several times in future projects a view-addon is worth the investment.
If you will use the views-interface for more then one-time-setup (the general use) e.g. define or change views in an editors/webmasters workflow this makes sense.
The basics of this is really simple and most probably a lot less coding and development then writing the views extensions.
/** Implementation of hook_menu().
*/
function gallery_menu() {
$items = array();
$items['gallery'] = array(
'title' => 'Gallery',
'page callback' => '_gallery_list',
'access arguments' => array('access content'),
);
$items['gallery/%gallery'] = array(
'title' => 'For dynamic titles, see title_callback documentation',
'page callback' => '_gallery_view',
'access arguments' => array('access content'),
);
return $items;
}
/** Load a gallery from database. Name follows %parameter_load hook.
*/
function gallery_load($id) {
return db_query("SELECT * FROM {galleries} WHERE id = %d", $id);
}
/** Render a list of galleries.
*/
function _gallery_list() {
$html = "";
$galleries = pager_query("SELECT * FROM {galleries}", 10);
foreach($galleries as $gallery) {
$html .= check_plain($gallery->title); //You would actually build vars here and push them to theme layer instead.
}
$html .= theme("pager");
return $html;
}
/** Load a gallery from database. Name follows %parameter_load hook.
*/
function gallery_load($id) {
return db_query("SELECT * FROM {galleries} WHERE id = %d", $id);
}
/** Render a list of galleries.
*/
function _gallery_view($gallery) {
$html = "";
$images = pager_query("SELECT * FROM {images} WHERE gallery_id = %d", 10, $gallery->id);
foreach($images as $image) {
$html .= check_plain($image->title); //You would actually build vars here and push them to theme layer instead.
}
$html .= theme("pager");
return $html;
}
Obviously, as stated in the comments, you would additionally create a few theme functions to handle the rendering, to 1) avoid hardcoded spagetty-HTML all over your module and b) allow the frontenders to stay in their theme when creating the HTML.
This sounds like a good opportunity to use an ajax callback. You could have your primary view on a part of the page just like normal and a secondary view in a custom block or something. when focus lands on the primary item (in the form of a button click or hover or something) you can use an ajax callback to replace the content of your custom block with the secondary view using your argument.
are you using drupal 6 or 7 for this? my understanding is that they do this is different ways.

Drupal 7: how to add template file to /node/add/content-type

In Drupal 7.
I want to themming /node/add/content-type by template file as page--node--add--content-type.tpl.php.
Please help me.
You need to use
page--node--add--content_type.tpl.php
instead of
page--node--add--content-type.tpl.php
underscore(_) instead of dash(-)
You will need to add the template as a suggestion in a preprocess function or you can create a custom theme function for the form, that will use the desired template. This is much similar to how it was done in Drupal 6, only you can do form_alters in your theme now.
you can add this function to your template.php:
you can print_r() your form and see what are the fields.
function yourThemeName_form_yourFormID_alter(&$form, &$form_state, $form_id) {
// Modification for the form with the given form ID goes here. For example, if
// FORM_ID is "user_register_form" this code would run only on the user
// registration form.
print_r($form);
}
You are adding tpl file for node form. For this you have to define tpl file name in template.php under your theme.
First create your_theme_name_theme()
Print content on tpl form.
Example: In below example contact_site_form is the form id of content type.
your_theme_name_theme() {
$items['contact_site_form'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'your_theme_name') . '/templates',
'template' => 'contact-site-form',
);
}
now create a file contact-site-form.tpl.php in your path location as specify above.
<?php echo render($form['name']); ?>
<?php echo render($form['mail']); ?>
.
.
.
<?php print drupal_render_children($form); ?>
You can render each element separately in given method.

Resources