Add HTMl to CCK within Drupal - drupal

Hello im a newbie at using Drupal, and have come across a block in my progress. I have been using CCK to add new content types to my forms. I was wondering if there was any way to add to the form that is generated so that i may contain the elements and also insert visual html code like head rules etc. I have dabbled with the hook_form_alter() and it does not seem to help me in my efforts. Ive been through adjusting tpl.php files and such and have made no progress. Please if there is any one there in the inter-webs who is knowledgeable on this matter your advice would be greatly appreciated.
Here is just an example of what I would want to do within the form:
1. Contain field elements within DIV's
2. Add HTML Content into the form

If you want to customize the markup for a for you can create a theme function for it and make the form use it with hook_form_alter. This has nothing to do with CCK which is used to customize a content type so you can add additional content to a content type.

hook_form_alter does not help you in altering an edit form containing cck fields because cck fields are added after hook_form_alter call.
You need to theme your form like this:
Create a module and put this hook implementation in it:
function mymodule_theme($existing, $type, $theme, $path) {
'your_form_id' => array(
'arguments' => array('form' => NULL),
'template' => 'my_template',
)
}
Then create a file named my_template.tpl.php in your module directory.
Now empty your cache.
In that file, you can render form elements separately, like this:
<?php
print drupal_render($form['a_form_element']);
print '<h2>blah blah blah...</h2>';
print drupal_render($form['another_form_element']);
// this one is for rendering all the remaining items, like hiddens:
print drupal_render($form);
Use devel module during development. It contains many useful tools that makes development much easier.

Raw HTML CCK Field (Drupal 6) - no filters, formats or editor
Simple Fix! Just use plain text format for unfiltered HTML. Then convert it back to html in a field .tpl when the node is built.
Plain Text format on a CCK field will convert the HTML tags to entity special characters (this would make is so it reads like code on the page instead of being actual html tags). It stores the string encoded using php's htmlspecialchars($text, ENT_QUOTES, 'UTF-8') inside of drupal's check_plain() function.
The cleanest simplest way to decode it, is in a field tpl file. This avoids hooks, hook order problems, looping bugs, and performance issues. This is done by adding tpl files to the base themes: hq_base, odyssey_base and odyssey_admin. Here is how drupal decodes plain text on a cck node edit form: print html_entity_decode(strip_tags($text), ENT_QUOTES); Note - html_entity_decode turns php tags into html comments when it decodes back to html. Here are sample files with the correct naming convention to give php control over the fields:
• content-field.tpl.php
• content-field-[your_field_name].tpl.php
content-field.tpl.php is a copy from the cck contrib into the theme folders, this is a contrib override to make it available in the theme, and should not be modified (unless you wanted to change all the fields in the theme). The field specific file is also a copy of the tpl, it will work once the override file is there. Then decode to html in the field tpl file:
• // print $item['view'];
• print html_entity_decode(strip_tags($item['view']), ENT_QUOTES);
Drupal Version Note:
The tpl files are slightly different in Drupal 7 and Drupal 8. But the html_entity_decode() is a php function that won't change per Drupal version.
Security Note:
This decode trick for getting raw HTML goes against the way Drupal was built for text formatting security. It means that anyone with permissions to edit the page now has permissions to edit html structure, and add script tags on the page. This can break layouts, and potentially be dangerous. You are relying on editing permissions for security here, instead of Drupal's normal Formats-per-Role security.

Related

Creating a link field in a custom content type

I have several custom content types and in one specific one I need to offer two fields, one for the href for a link, and one for the text of a link this way I can build and style it with minimal user involvement of the HTML/CSS. I also have a custom node.tpl for this content type. My problem is that drupal throws divs around each field I create that aren't in my template file for this content type (node-custom.tpl) and I can't put the href for a link with divs around it inside google.co.uk</div>"> See my problem. Maybe I'm doing this all wrong so any other ideas are welcome.
Please note I'm trying to create this site with minimum involvement of HTML/CSS access for the user. I'm aware I could hand code the link in the field.
The easiest way to do this would be to use a preprocess function in your template.php file and build the link up manually:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type = 'my_type') {
$uri = $node->field_name_of_link_field[LANGUAGE_NONE][0]['value'];
$text = $node->field_name_of_display_text_field[LANGUAGE_NONE][0]['value'];
$vars['my_link'] = l($text, $uri); // Using Drupal's l() function to render a link
}
}
Then in your node template file you'll have access to the variable $my_link which you can output anywhere, and will contain the correct HTML for the link. Finally, go to the "Manage Display" page for your content type and set the display of the two fields you no longer need to output to 'Hidden'.
There are other ways so if that's no good let me know
EDIT
Just to add, I think the easiest way to do this would actually be to install the Link module and use the provided field type instead of the two other fields you're currently using.

Output CCK items in custom template

I am working with a Drupal 6 site which relies heavily on CCK and views and I want to use the content in a webapp for iPhone. The webapp is built in HTML and CSS and will ultimately be converted to native using PhoneGap or similar and I want to import the formatted HTML using a JQuery ajax call. The plan is to create a view in the website which returns the data that I want for each page and then create a template for that view which outputs the data in the correct format for including.
I need to be very prescriptive with the HTML output which is where I am struggling. I'm not overly familiar with Drupal templating and what I have done seems like a lot of HTML is created when outputting a node, and it is all also contained within the site template. I need to be able to output exactly what I want e.g.
<div class="customclass">
<?php echo($cckfield) ?>
</div>
<?php foreach($cckarray as $key -> $value) {
echo(some html using $key and $value);
}?>
Hopefully the pseudocode above gives an indication of the level of control required. Is this possible using either core or with a module? Or do I need to roll my own pages and use the API rather than going for Views?
The problem with this is that there are many files collected together and merged into the output file. If you want clean HTML, you need to go through the different files that make up the final output (e.g. page.tpl, different modules files,...).
The best way to change the base HTML-output is to filter everything that you don't need out of the $node object that every site gets passed. It also holds all the CCK values in formated and unformated form as well as all other fields. SO creating a custom template with no HTML and the bare values of $node should give you what you want.

Add HTML to node title in Drupal module, not in theme layer

I want to add some functionality to my Drupal 6 module: I want to insert an image next to certain node titles (it involves a span and an img tag). I thought I could do this by just implementing mymodule_preprocess_node() and modifying the title. However, Drupal strips out all tags to avoid XSS attacks.
I've seen many solutions that involve the theme layer (most commonly http://drupal.org/node/28537), but I want to do this in my module, not in the theme. I don't want to modify any .tpl.php files or template.php. Can anyone give me tips on how to do this?
You mention that you've tried preprocess_node(), and are correct that, if you are storing the img tag as part of the node title, Drupal will indeed strip that out, as it runs $node->title through check_plain in template_preprocess_node().
My suggestion would be to store the actual image as an image field (using some combination of the imagefield module and imagecache for sizing), set the display of that field to be hidden on the CCK display tab for the given content type, and then attach the image to be part of the $title variable in your module's preprocess function. This solution would also allow you to display that image next to the title in any views you may need to create.
By 'certain node titles' - do you mean all nodes titles from certain node types?
If so, you can likely style the node using only CSS. By default all nodes will have a class that corresponds to the node type. Using CSS background images, you can add an image to the node title.
Your module can call drupal_add_css and add in any required CSS into the page. This way, it is theme independent.
I think the easier way is with javascript / Jquery.
You create a Jquery script which is called only in certain types of nodes and pass the number of views from drupal to the jscript.
You can call drupal_add_js() inside your module_preprocess_node and pass a variable which contains the number of views or the image link to the script. Something like this:
<?php
drupal_add_js("var mymodule_imagelink = " . drupal_to_js($imagelink) . ";", 'inline');
drupal_add_js("my_js_file.js");
?>
then in my_js_file.js just change the text. There are several ways to acomplish this. For instance, you can do a search in the document and change the title to something else or you can use a determined ID, etc...
Find text string using jQuery?
http://api.jquery.com/replaceWith/

How do you modify the fields output in Drupal's RSS Feeds

Trying to modify the RSS feeds created by Views module in Drupal.
Since there are no 'theme_' hooks for the RSS feeds (rightfully as XML is theme-less), I need an alternate way to modify the fields that are output into the RSS, preferably using template.php if possible.
http://api.drupal.org/api/function/format_rss_item/6 looks promising as that is where each row is created, but it doesn't
node_feed() is what collects the nodes, creates the additional fields, and then calls format_rss_item().
Specifically, we need to remove the dc:creator element from the $extra array created in node_feed()
If you go into a specific content type, you can set the RSS display fields as per this post:
http://redfinsolutions.com/redfin-blog/show-hide-fields-views-generated-drupal-rss-feed
I am adding another answer, as I have recently had to do this and managed to do it without modifying data in the theme layer.
You can add a preprocess function to your view. It is a bit of work though.
There are some guidelines here, but they are a bit confusing. Here is my summary of how to do this.
First make sure your modules weight is > views
Secondly copy the template you would like to add a preprocessor to to your modules directory. Rename it to be something in the list of templates in theming information.
Then edit hook theme like this (but change to use the existing view that you need to override.
function mymodule_theme($existing, $type, $theme, $path) {
// Copy the exsisting views theme from the existing array.
$override = $existing['views_view_row_rss'];
// Add my preprocessor to the list of preprocess functions
$override['preprocess functions'][] = 'mymodule_myfunction';
// Point to a template so that this view will be used.
$override['template'] = 'my_more_specific_template_name';
$override['path'] = drupal_get_path('module', 'path');
unset($override['file']);
// Return your theme handler.
return array('my_more_specific_template_name' => $override);
}
You should then be able to write your preprocess code in the function mymodule_myfunction.
In the view, if you click on "style information" this will show you the template files used to create the feed. You can copy the template so that it is overridden for your view and remove dc:creator from the $item_elements array.
This is not particularly nice as you are modifying data in the theme layer, but it will do what you want.
I'd suggest using the Views Node Feed module to do this. It will let you completely write the XML that is output by Views for feeds.

How do you remove the default title and body fields in a CCK generated Drupal content-type?

When you create a new content type in Drupal using the Content Creation Kit, you automatically get Title and Body fields in the generated form. Is there a way to remove them?
If you're not a developer (or you want to shortcut the development process), another possible solution is to utilize the auto_nodetitle module. Auto nodetitle will let you create rules for generating the title of the node. These can be programmatic rules, tokens that are replaced, or simply static text. Worth a look if nothing else.
To remove the body edit the type, expand "Submission form settings" and put in blank for body field label. For title you can rename it to another text field. If you really have no need for any text fields you can create a custom module, say called foo, and create function foo_form_alter() which replaces $form['title'] with a #value when $form['type']['#value'] is your node type.
No need to install anything:
when editing the content type, press "Edit"
(on the menu of Edit | Manage fields | Display fields )
click on the Submission form settings
on the Body field label:
Leave it blank, it would remove the Body field.
If you're not a developer (or you want
to shortcut the development process),
another possible solution is to
utilize the auto_nodetitle module.
Auto nodetitle will let you create
rules for generating the title of the
node. These can be programmatic rules,
tokens that are replaced, or simply
static text. Worth a look if nothing
else.
And to add on to William OConnor's solution...
The module is poorly documented unfortunately. It's really only effective if you use PHP with it in my opinion. Check off the "Evaluate PHP in Pattern" and type into the "Pattern for the title" field something like:
<?php echo $node->field_staff_email[0]['email']; ?>
or:
<?php echo $node->field_staff_name[0]['value'] . '-' . gmdate('YmdHis'); ?>
...where I had a field with an internal name of "field_staff_email" and was using the CCK Email module -- thus the 'email' type was used. Or, I had a field with an internal name of "field_staff_name" and was just an ordinary text field -- thus the 'value' type was used. The gmdate() call on the end is to ensure uniqueness because you may have two or more staff members named the same thing.
The way I discovered all this was by first experimenting with:
<?php print_r($node); ?>
...which of course gave crazy results, but at least I was able to parse the output and figure out how to use the $node object properly here.
Just note if you use either of these PHP routines, then you end up with the Content list in Drupal Admin showing entries exactly as you coded the PHP. This is why I didn't just use gmdate() alone because then it might be hard to find my record for editing.
Note also you might be able to use Base-36 conversion on gmdate() in order to reduce the size of the output because gmdate('YmdHis') is fairly long.
The initial answers are all good. Just as another idea for the title part... how about creating a custom template file for the cck node type. You would copy node.tpl.php to node-TYPE.tpl.php, and then edit the new file and remove where the title is rendered. (Dont forget to clear your cache).
Doing it this way means that every node still has a title, so for content management you aren't left with blank titles or anything like that.
HTH!

Resources