Drupal form editing and conditional fields - drupal

I have the following in my template.php file:
function theme098_theme() {
return array(
'email_node_form' => array(
'arguments' => array('form' => NULL),
)
);
}
and...
function theme098_email_node_form($form) {
return drupal_render($form);
}
I've excluded the code where i actually modify the form and cut it down so that no modifications happen. Two problems occur:
The order of items (i.e. their weights) is messed up. The save button is at the top etc. Even if I try to edit the form by setting the weight, the save button STILL appears at the top.
The real problem: Conditional fields doesn't work. For some reason, I think this overwrites what other modules are supposed to do? I'm not sure
Can anyone shed some light?

3 things.
In this case you should probably use hook_form_alter(), if you want to change the order or change the form, instead of using a theme function to alter it. Keep presentation and logic separated.
When you define theme functions with hook theme, you should call them theme_xxx instead of themename_/ modulename_.
Modules (and possible themes) have a weight weight that determines the order they are run with functions like hook_*_alter. Look at the install file for the devel module to see how this is done.
One or more of these things should help you out.

Related

Stop filtering WordPress function wp_insert_post

I have to make a few minor changes to a website built in WordPress, and I came across some problems with the wp_insert_content function. This function sanatizes input, taking some HTML tags completely out (like ) and deleting attributes on others. I would like to switch this off, and everywhere I find a link to http://pp19dd.com/2010/06/unfiltered-wp_insert_post/ where the solution is to add
'filter' => true
to the posts input array.
This article is from 2010 though, and from what I found an extra line was added in 2011 (http://web.archiveorange.com/archive/v/TDTh42SUwDEc1GFmSrvU). This line reads:
unset( $postarr[ 'filter' ] );
and is called right after merging the input with the defaults, and before sanitizing. It seems to me that this line cancels the 'filter' => true statement above. Indeed, sanitizing happens if the line is there, and disappears if the line is taken out.
So the easy solution would be to add the 'filter' => true and delete the extra line in the function. Problem though is that I have no idea where else the function is used, and I wonder if it's smart at all to hack right in the WP code. An update of WP would restore it anyway. So ... is there any other way to stop this function from sanitizing the input?
It is possible that some other plugin may be trying to sanitize the content, in such case remove all the filters is the only option, try the following:
remove_all_filters("content_save_pre");
$post_id = wp_insert_post($post);
Other possible filter tags are:
pre_content
pre_post_content
content_pre
Try them one by one also if it does not work.
i'm also in the process of importing posts with s in the content body.
i've been searching all morning for a solution, there have been some discussions on the topic. so far
kses_remove_filters();
has done nothing for me, neither did commenting these two lines:
unset( $postarr[ 'filter' ] ); // overrides 'filter' => true since 3.0 i guess
$postarr = sanitize_post($postarr, 'db');
in wp_insert_post() in post.php (defaults at line 2704).
there's another interesting bit at line 2874:
$data = apply_filters('wp_insert_post_data', $data, $postarr);
which seems like could be the source of our troubles, but i'm too much of a newbie to figure this out on my own.
really, truly, desperately need help! oh, and i can't comment, sorry for the spam action.
i would like to keep this thread open and find a solution.
I guess it worked before i posted, but the content i was importing already had HTML tags stripped
the easiest workaround would be to manually overwrite the content after inserting the post, but this only works for isolated environments or for importing
$wpdb->update($wpdb->posts, array("post_content"=>$content->content), array("ID"=>$postid), array("%s"), array("%d"));
anyhow,
remove_all_filters("content_save_pre");
did the trick!

Symfony2 and Twig Dump Issue

I am running into an issues with dump() in Twig.
I am not able to completely dump the values of the object that I am returning to my twig template. My object, as defined below, is built up of a product object, qty key/val, OnOrder key/val and avgUnitCost key/val.
I AM able to use dump(qty), dump(OnOrder), dump(avgUnitCost) and see the values of these.
I AM NOT able to use dump() on product to see the key/val of the product object. All I get is a white page of death.
I have read elsewhere on stack that it is a memory issue in the php.ini file. This does not seem to fix the issue, I set mine 1024M and it still times out and gives me the white screen.
I have also read this guys article on the same issue: http://hectorpinol.com/twig-debug-in-symfony-2/ ... He thinks it is a "bidirectional association problem".
In any case, here is the code that I am using to pass the object and render my twig template...
return $this->render('TestBundle:Event:view.html.twig', array(
'heading' => 'View Product',
'product' => $product,
'qty' => $qty,
'OnOrder' => $OnOrder,
'avgUnitCost' => $avgUnitCost,
));
Here is the guts of my question:
How can one effectively use twig to access the elements of an object, whether it be dump or some other method. I need to be able to see all of the elements in the object so that I can place them on the page as I need.
Thanks so much for your help!!!
Check LadybugBundle. You can dump everything.
Try adding a break point in twig_var_dump:
/vendor/twig/twig/lib/Twig/Extension/Debug.php (at the bottom)
Then you can use the functionality of your debugger...
Look at this answer: https://stackoverflow.com/a/29302069/4102223
It is my approach to solve this problem, only few lines must to be changed in one place (it is easier because no need to include new bundles and read its documentation).

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!

Remove or Filter Options on Exposed Filter in Drupal 7

I'm working on a website that uses Domain Access and for a view I expose a filter that lists all domains. I want to list only some domains but not all. I know that it is posible to do with the filter configuration, but if I limit the options this way there is a problem with ajax which changes all the options text to "1", and in any case I want to know how to do it, with either hook form alter or with themes, but preferably using some hook from a module.
In general I'm trying to figure out how to remove some of the options for an exposed filter, I managed to do it using JQuery, but I'd like to do it with php, thanks!
It should be fairly straight forward using hook_form_FORM_ID_alter as you suggest. First inspect the <form> element of the filter form (using Firebug or similar) and get it's ID attribute. The form's ID in Drupal will be that string but with all '-' characters replaced with '_' (so 'form-exposed-filter-form' would become 'form_exposed_filter_form').
The thing to remember is that the submit handler for the form will probably be expecting the elements you're trying to remove to be there, so you'll probably get unexpected results if you just yank them out of there.
The way round it is to change the type of the elements to value instead (so they're available to the submit function in $form_state['values']), and choose a default value for each. Something like this:
// Replace 'FILTER_FORM_ID' with the form's ID
function mymodule_form_FILTER_FORM_ID_alter(&$form, &$form_state, $form_id) {
$form['some_existing_element'] = array(
'#type' => 'value',
'#value' => $default_value_for_filter
);
}

Drupal Views - How to return information about a view that is being rendered

I'm having trouble dealing with hooks for the Views module in Drupal. What I'm trying to do is determine which view is being rendered so that I can identify it and make changes to it. IOW, I don't know ahead of time which view I'm working on.
In the code below, I've replaced my actual module name with "MODULENAME".
In my .module file I have included a file MODULENAME.views.inc file with the following:
include_once ( dirname(__FILE__) . '/MODULENAME.views.inc');
In the .views.inc file, I have a MODULENAME_views_api function like this:
function MODULENAME_views_api() {
return array('api' => 2, 'path'=> drupal_get_path('module', 'MODULENAME'),
);
}
Those seem to work just fine. So, now I try to get down to business with an actual hook...
function MODULENAME_views_pre_render(&$view) {
$get_view_info = $view->name;
echo $get_view_info;
}
MODULENAME_views_pre_render();
The problem is this throws an error, "Missing argument 1 for MODULENAME_views_pre_render().
So, obviously it expects me to pass in an identifier of some sort to tell it which view I want. But that's the whole point of this function is to determine which view is being rendered. If I knew the answer to that, then I wouldn't need to call the function in the first place.
Am I missing something obvious? Is there a function call that I can use to return this identifier?
You hook into things by implementing hooks, so this part of your code is ok:
<?php
function MODULENAME_views_pre_render(&$view) {
$get_view_info = $view->name;
echo $get_view_info;
}
But this:
<?php
MODULENAME_views_pre_render();
Why? You're not generating a view, Views is. It's not your job to invoke the hook. You just implement it.
So, you need to make changes to the view? you do it right there:
<?php
function MODULENAME_views_pre_render(&$view) {
if ($view->name == 'TheViewIWantToModify') {
// Make some changes to the $view
}
}
And that's it.
Also, note that depending on the type of modifications you want to do, you might want to implement another hook instead of hook_views_pre_render(). Take a look at the docs/docs.php file that comes with Views (version 6.x-2.12 at least, I don't know which version you have, and BTW you should indicate this) and starting on like 538 you'll see a few hook_views_pre_ and hook_views_post_ type of hooks (that is, their descriptions, for you to know what each of them are good for), and then you can decide which one to implement in your module.

Resources