How to change Drupal input filter behaviour based on output location - drupal

I have a Drupal filter module whose output I would like to alter, depending on where the output is going to be displayed. Specifically, I want to the filter to give the full output for nodes, but trim the content down for blocks.

I don't think this would be possible. It's hard enough to figure out what context something is being displayed in. It's doable but quite hard to code on your own. However the way the filter system works I don't think its possible within a filter to determine then context of the text being filtered. It's simply not made for something like that.

I'm the OP (but just registered an account).
I did manage to find a solution/workaround. Here's what I did:
Create block.tpl.php in my module which is a copy from system/block.tpl.php, with a constant added at the top.
Put my template file at the head of the theme registry using hook_theme_registry_alter():
function hook_theme_registry_alter(&$theme_registry) {
// using our own block.tpl.php file.
$theme_registry['block']['template'] = 'block';
$theme_registry['block']['path'] = drupal_get_path('module', 'module_name');
$theme_registry['block']['type'] = 'module';
$theme_registry['block']['theme path'] = drupal_get_path('module', 'module_name');
$theme_registry['block']['theme paths'] = Array();
}
Checked for the constant while constructing the filter output, changing as necessary.
Celebrated the outcome.

Related

RssDisplay Extension / Simple Pie

I'm just a little desperate.
I installed the RSS Display extension.
http://typo3.org/extensions/repository/view/rss_display
Everything works fine, but I have just a little understanding problem.
I pull myself with fluid the Author for the current feed.
<feed:item.get value="author"/>
Than i look whats inside.
<f:debug><feed:item.get value="author"/></f:debug>
Thats the result.
SimplePie_Author prototype object
name => 'Name Name' (12 chars)
link => NULL
email => NULL
So what i need it's to get the name of the author.
Unfortunately i am not able to get the value.
I am really new in Fluid, Typo3.
Hopefully someone can help me.
One way to do this, is to create a fluid variable author and assign the author object to it. Then you can access the name using {author.name}.
To create a variable, you could use the ViewHelper <f:alias>, like this:
<f:alias map="{author: '{feed:item.get(value: \'author\')}'}">
{author.name}
</f:alias>
Another way would be to use the extension "vhs", which provides many tools for use with fluid. One of these tools is the ViewHelper <v:variable.set>, which could be used like this:
<v:variable.set name="author" value="{feed:item.get(value: 'author\'}"/>
{author.name}
This has the advantage that you don't need to use the variable within the tags of the ViewHelper.
There are other ways to reach the same goal, without defining variables, but this seems to be the easiest one to me.

In Drupal 7, how do I get a list of all blocks being used on the page?

I'm building a module that manages ad units in the form of blocks that all need to be aware of each other and pass information around.
Thus I need to find a simple hook or other function to get a listing of every block that will be used on the page, so I can be sure to know the entire list of ad units on the page.
I've tried hook_block_list_alter() but this seems to return the ENTIRE list of blocks that exist in my Drupal install, with no designation of which blocks are actually going to be rendered or not on this page.
So, now what do I do?
I think it's got something to do with the order hook_block_list_alter() is called in but I'm not entirely sure. Either way the following code will get you a list of all blocks in the current page context. Be sure not to put this in a hook_block_list_alter() function or you'll get an exception caused by infinite nesting.
global $theme;
$all_regions = system_region_list($theme);
$blocks = array();
foreach (array_keys($all_regions) as $region) {
$blocks += block_list($region);
}
If you only need the blocks in a particular region you can ditch most of the code above and just go with:
$blocks = block_list('region_name');
I ran into the same problem when calling block_list('content'). Looking at the code for this function I found it calls two other functions, _block_load_blocks() and _block_render_blocks(). The problem seems to occur in _block_render_blocks() as the display text is not added to the content object. This is different from the other block objects that pass through the function.
To get around this, instead of calling block_list(), I called _block_load_blocks() directly. This returns an array of blocks grouped by region, bypassing the _block_render_blocks() call.
Now we can check for blocks in the content region without the content text disappearing. Huzzar!
I had a similar requirement when implementing an adserver (DFP). My solution was to define an array as a global variable, and include php code in each ad unit block that added a new element to the array.
Then, once all blocks on the page have been executed, you can simply access the global variable to see which ad blocks were called. Because the code to build the list of blocks being called is part of each block, it doesn't matter whether the blocks are displayed in a region, in a panel, or anywhere else.
In my case, I wanted to use the information to add scripts to the <head> section that reference only the add units from the blocks being placed. My complete solution was as follows:
1) Implement hook init to create a global variable in which to store information about which blocks are being displayed (you need to create a custom module to contain this code):
YOURMODULE_custom_init() {
$GLOBALS['dfp-ads'] = array();
}
2) Enable the core php module
3) Add php code at the end of each ad block to add a row to the array created in step 1
<?php
$GLOBALS['dfp-ads']['AD_OR_BLOCK_NAME_GOES_HERE']="AD SPECIFIC SCRIPT GOES HERE";
?>
4) Implement THEME_preprocess_html in my template.php file to access the global variable, build the script, and add the script to <head> section with a call to drupal_add_html_head
function YOURTHEME_preprocess_html(&$vars) {
$inline_script = LOGIC TO ACCESS $GLOBALS['dfp-ads'] AND BUILD SCRIPT GOES HERE;
$element = array(
'#type' => 'markup',
'#markup' => $inline_script,
);
drupal_add_html_head($element, 'google-dfp');
}
It sounds from your description that you don't need the list of ad blocks to build javascript for the head section, but instead want to use the information to modify the contents of the blocks themselves.
In that case insteaad of THEME_preprocess_html, you could try hook_page_alter(&page)
The api page for that hook states that individual "Blocks may be referenced by their module/delta pair within a region:"
// The login block in the first sidebar region.
$page['sidebar_first']['user_login']['#block'];
Hope that helps someone!

Drupal Panel Pages Pathauto

I have a Panel Page set up with the path node/%node/foo and all works fine when I visit a link such as node/6/foo. However, when I visit nodealias/foo it doesn't work at all. Is it possible to get panels to work with pathauto in this way?
I am thinking I may have to implement the hook hook_url_inbound_alter and change the url myself.
I also posted a support request in the panels module here: http://drupal.org/node/1219796
As Alexey answers panels doesn't care about aliases, it only sees node/%nid
Here's a good explanation that is valid still for D7:
http://drupal.org/node/211338
To summarize and bring it up to date for D7:
Export your variant for the panel you've created and import it into the panel that overrides the default node display in Drupal.
Add Criterias to the variant so the Panel/variant is only used for the type(s) of content you want to display with this variant.
Voila :) (read the discussion at the link, else the summary will be difficult to understand)
Hope this helps - I myself have spend some time googling and trying to understand this, and I was also confused by the fact that Views DOES care about aliases...
I fixed this using the following code, you would need to alter the pattern to match the pattern of your url aliases and alter the function name to match your module's name.
function brooklands_url_inbound_alter(&$path, $original_path, $path_language) {
$pattern = '#^works\/[A-Za-z0-9]+(-[A-Za-z0-9]+)*\/images(\/\d+)?$#';
if(preg_match($pattern, $original_path)) {
$snip = substr($original_path, 0, strrpos($original_path, '/images'));
$system_path = drupal_lookup_path('source', $snip);
if($system_path) {
$tail = substr($original_path, strrpos($original_path, '/images'));
$path = $system_path . $tail;
}
}
}
You can use this module Subpathauto
it automatically makes the alias to work with subpaths, such as: nodealias/foo
The nodealias is the full alias of your node with nid=6. The third argument (foo) is added through hook_menu() by panels module to the exact alias (node/%nid/%anythingelse) and it is NOT applied to your aliased URL, so you can not use nodealias/foo url to access your panel just because it is not 'hooked' by panels module.
Changing url manually is a good idea, I think.

How to restrict text length of a field while in WordPress editor?

I would like to restrict the fields while creating a new post in WordPress. For the title, it should not exceed 40 characters. For the content, it should not exceed 400 characters. If these maximum values are exceeded, I would like to show an error message and not let the user continue. How do I do that in WordPress?
You should be able to use wordpress filters to modify the code that gets outputted when the editor is called. Essentially you would want to use it to insert some javascript and an extra div tag to display your error, then just read the contents of the "editorcontainer" id and show the error once it reaches a certain character limit.
I don't have the time at the moment to write a case example, but depending on your skill level, the functions you are looking for are:
apply_filters("the_editor", "customfunction_limitarea");
Where customfunction_limit area is your created function to insert the javascript. You can see how the_editor is currently called and how the default filters are applied in "wp-includes\general-template.php" on line 1822. The default looks like this:
$the_editor = apply_filters('the_editor', "<div id='editorcontainer'><textarea rows='$rows'$class cols='40' name='$id' tabindex='$tab_index' id='$id'>%s</textarea></div>\n");
I would try modifying that statement by placing a new filter in a functions.php file located in your themes directory, that way you don't have to worry about it getting over-written during an update. Otherwise, if you absolutely have to edit the wordpress core (generally a no-no), general_template.php would be the place to do it I think.
Essentially just read up on wordpress filters a little bit (be warned there's not a ton of documentation or examples available for it other than the basic stuff), and that should provide everything you need. The input verification end is easy enough to find scripts, just google jquery post limiting. Something like this might be exactly what your looking for:
http://swiki.fromdev.com/2010/02/jquery-maximum-limit-texttextarea-with.html

How to read the filter variable in a php code block in views 2?

I'm trying to create an img link manually with php in the header field of a view, in drupal 6. I need to read the value of one of the filters, but can't find the right variable to read. I thought I could print_r($view->filters) but it didn't give me anything, and I eventually found out that isset($view) is false.
Am I looking the wrong way?
The context I'm writing in is the header field of the view, with php code as input format. Do I have to "enable" the $view variable for reading in this context somehow?
OK I found it, it was really easy:
$pa_view = views_get_current_view();
$pa_nr = $pa_view->display['default']->display_options['filters']['field_nummer_value']['value']['value'];

Resources