Output CCK items in custom template - drupal

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.

Related

Render a Wordpress page as a variable

Is it possible to get Wordpress to render a page and return it as HTML ?
I'm thinking about a function that'd look like this :
$html = render_page($id);
echo $html; // prints the page
My goal is to have all my sections' HTML and my corresponding logic in different files and, in my home page file, merge all the different sections in the order I wish. Thus making a one-page kind of website.
Because you define a template for a page, I don't see why it's doesn't seem to be possible to do this.
I have finally found a workaround for doing exactly what I wanted :
I have all my different "pages" (they are Wordpress pages, but I use them as different sections on a one-page site) in different files.
Each file has it's own HTML and corresponding logic.
In my index.php file, I call my files this way :
require_once(locate_template('tpl-name.php'));
I'm pretty sure it's not exactly the best way of doing it, but it works for me. Wish Wordpress would allow API access to it's rendering though.

Create an Iframe from a Drupal Website

I have a drupal website. I want to generate an Iframe with content from my drupal site, that other sites can embed.
How I think this can be achieved:
Method 1: Create a php script that is autonomous from the drupal engine.
Import the configuration file and hence gain access to the database. Generate the content as a standalone webpage. Distribute the url of this script as the iframe's source url.
Problems: cannot provide drupal functionality within the iframe such as interaction with logged in user.
Method 2: Create the iframe from within drupal.
Create a new module that defines a menu entry using hoom_menu (the url for the iframe). Define the content of the iframe from the callback function of the menu entry. Then Somehow assign a custom page.tpl.php theme for the desired iframe url so that only the content of the iframe is rendered without all the other page elements (blocks, menus, footer, etc.).
Any comments especially for method 2 will be greatly appreciated! :)
I have done exactly this, just this week!
I created a custom module that outputs only the content that I want (using hook_menu()). Then I created a page template (page-mycustommodule.tpl.php) that only has
<?php print $content; ?>
within the <body> tags.
That was basically all. To find out the name that your page template needs to have, use the devel and theme_devel modules, then just click on your page and it will tell you which templates it looked for.
One thing to look out for: any links in the iframe will only change the contents OF THAT FRAME, so when your module creates links, use the proper target to make the parent page jump to the new URL:
l('link text',
'node/' . $mynode->nid,
array('attributes' => array('target' => '_parent')));
I found the answer by Graham very useful. Unfortunately I don't have enough reputation on this site to add a comment so I will have to put my comment in an answer.
5 years on, the information has changed slightly:
The module theme_devel now seems to be called devel_themer instead.
In D7, the template naming pattern is slightly different with 2
hyphens: page--[front|internal/path].tpl.php (see docs)
D7 templates are slightly different based on render arrays, so the template will need to be something like print render($page['content']);
method 3
you can use this module https://www.drupal.org/project/entity_iframe that allows you to create IFRAME READY webpages
install it and go to the display settings of you content type that you want to use as iframe content admin/structure/types/manage/CONTENTTYPE/display
choose the IFRAME view mode and choose the fields you would like to be shown
and then use url like this domain.com/entity_iframe/node/NID and you will have a display with no extra headers footers etc ...
By default a sample EMBED/IFRAME code is provided to the admin under each node the settings
<iframe width="100%" height="300" src="domain.com/entity_iframe/node/96" frameborder="0" class="entity_iframe entity_iframe_node" id="entity_iframe_node_96" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
The settings in admin/config/system/entity_iframe control some of the details of the embed code
For complete control of the theme used you can use in combination with https://www.drupal.org/project/entity_iframe_theme
What do you exactly need to iframe?
A node? A block? Should it be static or dynamic?
You can simply create a node with a php filter and generate the iframe output.
Then you can put this output between <pre> tags to display it as code that users can copy/paste in their site.

How do i find the id of a drupal block-id if i know it's block-description

In a specific template i want to use a block, so it's contents can be edited by an editor for all content using that template.
Because the setup is multisite i cannot use the block-id(bid). The id of the block is different for different sites.
Is there a way retrieve a blockid for a block description via the API?
I could use a SQL query on the prefix_boxes table, but would prefer to use a documented API.
I'm currently using the code below on Drupal version 6.
$block = module_invoke('block', 'block', 'view', 7);
print $block['content'];
You can use block_list (Here is the documentation for it: http://api.drupal.org/api/drupal/modules--block--block.module/function/block_list/6) to retrieve the block list per region, there you should find your block id by the description.
But writing your own query isn't bad solution in this case, For my opinion at least.
You do not want to use the block, but a region itself. You can achieve this using the _preprocess_nodehook, adding the region in the .info and printing the region in your template.
By the way, the trick than Ran published is quite interesting as you can use this to write your region in virtually any part of a Drupal site.

Add HTMl to CCK within 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.

Drupal: include small form in page.tpl.php

In my template.php i have a function_name($form) with some basic html markup and a drupal_render($form['mail']) and drupal_render($form) that returns $output;
I'd like to include this small snippet & form in my page.tpl.php. Everything else in page.tpl.php is printed out as a single variable, so I'd like to do that for the $output of the function above.
What is the best way to do this?
In theory you could append the output of the function to the $content variable.
However, It sounds like you have something which may be better off as a block, rather than something that exists in your template.php.
You can quite easily create a module with a single block (which would be your function). You could add this where ever you wanted and easily turn off and on (and all the other block admin goodness) without having to change the code.
The other thing you could try out is the webform module. It's a nice module that has a large feature set, saving you the trouble of having to create a module. You can have a webform block which you can stick on that particular page (restrict to page).
I also see that your form is called mail. Webform also allows you to email form submissions. In addition, it provides a nice interface to see what users have submitted.

Resources