I'm looking for a good, standards friendly way to alter the default comments form, such that there is a disclaimer immediately below the "Reply" header. I only want this disclaimer to appear above the comments form itself, not meerly when viewing comments.
This thread ( Drupal: adding disclaimer text above the submit button in a webform ) partially answers what I want but I'm not sure about how to apply the solution specifically to the comments form.
I know that this could be a rank amateur question, but any and all help is appreciated. Thanks.
Edit:
I've tried implementing hook_form_alter as suggested and have managed to get the disclaimer to appear within the form just fine. One problem: the first draft of my disclaimer seems to be trapped above the comment form when replying to a comment. Clearing the cache, resetting the theme registry (on every page load, thanks to devel module) all have no effect.
Reply to Comment:
[first version of disclaimer] // won't go away, ever
[comment form]
[current version of disclaimer] // this one is fine
[submit button]
Any help here would again be most appreciated.
Edit (redux):
Implemented the template.php centric solution. It was able to work just fine without any of the external effects as described above. Still not sure about a solution to the above problem. Thanks everyone.
The comment form is a unique animal among Drupal forms in that it's not themeable by default, so the usual methods won't work without a little bit of extra help.
In short, you'll first need to register the form as themeable in your template.php file:
/**
* Implementation of hook_theme().
*/
function mytheme_theme(){
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
Next, you can add a theme function to drop in some additional elements to the form:
/**
* Theme the output of the comment_form.
*
* #param $form
* The form that is to be themed.
*/
function mytheme_comment_form($form) {
$form['new_element'] = array(
'#type' => 'markup',
'#title' => t('Disclaimer'),
'#value' => '<p>You have been disclaimed, sir!</p>',
'#weight' => -20 // Lighter elements float to the top of the form
);
return drupal_render($form);
}
An understanding of how the Forms API (http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html) works will help you through making these edits. But, if you're just adding some HTML it should be pretty easy.
Of course, be sure to rebuild your theme registry after you've added the theme function.
While what anschauung has posted will work, it's a lot of extra work compared to using hook_form_alter in a custom module. In your case, it would look something like this, using the close related hook_form_FORM_ID_alter:
function module_name_form_comment_form_alter(&$form, &$form_state) {
$form['disclaimer'] = array(
'#value' => t('Disclaimer text'),
'#prefix' => '<div>',
'#suffix' => '</div>',
'#weight' => 0,
);
}
In the above example, you need to replace module_name with the name of the custom module you create.
The #weight will determine the placement, the lower it is the heigher up it will be placed. So tweak it until the placement is right.
#prefixand #suffix is the markup that is created before and after the element, can be anything.
#value the value of the form element, the actual disclaimer text.
This form element hasn't a #type, so it uses the default which is markup, which means it's just some custom html.
Related
I'm having a hard time trying to make tiny mce editor work in the wordpress customizer.
It looks like just calling "wp_editor" from my WP_Customize_Control extension doesn't trigger loading of TinyMCE scripts in the customizer.
I've tried to load them manually like in this answer here: https://wordpress.stackexchange.com/questions/175307/tinymce-is-not-defined-when-not-using-wp-editor
and also tried with wp_enqueue_script('tiny-mce') but to no avail.
Here's my render method, nothing fancy:
public function render_content() {
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<?php
$settings = array(
'media_buttons' => false,
'quicktags' => false,
'teeny' => true
);
wp_editor($this->value(), $this->id, $settings );
?>
</label>
<?php
}
Any suggestions on how this should be done right?
I managed to make it work. See my answer with a conclusive code sample on workdpress.stackexchange.
p.s. I too resorted to calling do_action('admin_print_footer_scripts') which is obviously a dirty hack as it includes a bunch of other unnecessary scripts on the customizer page but does the trick awaiting for a more elegant solution..
Ok, found out one solution but using wp_editor in the customizer means opening pandora's box.
Adding do_action('admin_print_footer_scripts'); before creating the editor:
do_action('admin_print_footer_scripts');
wp_editor($this->value(), $this->id, $settings );
will have TinyMCE get rendered. However, issues that still remain are:
updates in the rich text box are saved to the corresponding textarea, however events like "changed" or "keyup" that the wp.customize is watching for to do the postMessage are not triggered by the rich editor but only when changing directly the textarea.
because of the above, the "Save" button will not get enabled.
artificial triggering of the "changed" and "keyup" events on the textarea are not caught by wp.customize, hence the update callbacks in the preview area are not called
once every several customizer loads the tinymce will not be rendered completely, probably due to the way the admin scripts are loaded
Therefore, after much digging on this topic I realized that the wp_editor is not yet ready to be used reliably in other areas than posts editing.
I am using CMB2 and would like to only show a particular CMb2 metabox on the default page template. I have tried passing a value of page.php as the value of the show-on filter but its not working. Any ideas welcome? I could write my own custom show-on filter but I am sure the functionality must already exist.
It's been a while, so maybe you've found the answer. But since I've been looking myself as well, let me post the result here for future reference.
The solution is simply, instead of 'page.php', just use 'default' as the value for the show-on filter:
'show_on' => array( 'key' => 'page-template', 'value' => 'default' )
#ruude3 answer works. Also you'll need to either save the new page as a draft or publish it before the meta fields will appear.
I have Drupal 7 site. It has custom content type Product. It has 20 fields. I want to control the UI of the add/edit form for this content type.
I created a module under sites/all/modules.
admin_product
-admin_product.info
-admin_product.module
-admin_product.module
<?php
function admin_product_theme($existing, $type, $theme, $path){
return array(
'product_node_form' => array(
'arguments' => array(
'form' => NULL,
),
'path' => drupal_get_path('theme', 'myTheme').'/templates/forms',
'template' => 'product-node-form',
'render element' => 'form',
)
);
}
In the templates - sites/all/themes/myTheme/templates/forms/product-node-form.tpl.php
-product-node-form.tpl.php
<?php
echo drupal_render_children($form);
echo 'hello template'; // just to test
?>
The template is not rendered.
How do I control the UI of the form.?
Any help highly appreciated.
I think all you need is Field Group module. With the help of this module you can organize all your fields in groups (i.e. divs, tabs etc.). The rest stuff can be made by CSS.
Other way is to use hook_form_FORM_ID_alter() hook, or, specifically, hook_form_node_form_alter(), to alter your form.
Anyway, in Drupal 7 Form API differs from Theme API, and the code you provided just has no sense :) Sorry for this :)
If the theme you're adding this to is not the default admin theme for the site then the template won't get picked up in the admin area.
You'll have to add this code to a custom module or create a sub-theme based on the administrative theme (e.g., Seven) and select that as your admin theme and put the code there.
Good explanation found here: https://drupal.stackexchange.com/questions/33253/how-do-you-theme-a-content-types-create-edit-form-in-drupal-7
My employer requires certain pages on the website have a two page feature.
What this means is that some default content show up on the node_view page as normal but the second part should show up when a link is clicked.
This will be easy if I could do this across multiple nodes but the requirement is for all the data to be stored in one node but displayed across two pages.
I hacked together the following code:
function mymodule_node_view($node, $view_mode, $langcode){
$path = current_path();
$path_alias = drupal_lookup_path('alias',$path);
$links = array( 'test' => array('title'=>'my_link', 'query'=>'', 'href'=>"{$path_alias}/nextpage") );
$node->content['my_module'] = array(
'#theme' => 'links__node__mymodule',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
}
That creates a hyperlink called my_link across the top of my content area - which is great.
The problem starts when I click the hyperlink. Supposing I am on http://example.org/homepage and I click the hyperlink, I expect to be redirected to http://example.org/homepage/nextpage. Also, I still want to maintain the view and edit tabs of the actual node I was on. However, Drupal correctly gives me a "page not found" error.
What's interesting is if I used http://example.org/node/1 and visited http://example.org/node/1/nextpage, I don't get the issues I described above but the url is less descriptive.
To solve this problem, I am sure I have to extend hook_menu but I don't know how to account for any number of taxonomy terms leading up to the actual node title. So, I can't predict how many % I will need before the node title and then my /nextpage. However, I want /nextpage to still have the view and edit tabs of it's parent page.
This is all unexplored territory for me.
Update
I found the following function which does a great job of returning the entire node path complete with taxonomies:
$path = current_path();
$path_alias = drupal_lookup_path('alias',$path);
What I don't know is how to take advantage of this in hook_menu to dynamically create /nextpage for my nodes.
Please remember, I don't really want /nextpage to be entirely independent of the original and actual Drupal node. When on /nextpage I want to be able to have access to the view, edit etc tabs of the node.
So, /nextpage effectively is just an extension of a Drupal node page.
There is a quick way to do that. Using views module.
In the fields section choose the field you wanna view. And in the arguments add the nid.
Then add the link to the node view you already created.
The final result http://mysite/views-page/[nid]
Hope this helps... Muhammad.
I would check the node_menu() function to get some reference on how it's implemented.
Not sure on your taxonomy requirements so this might be insufficient but I'll go with what I understand.
But off the top of my head I'd go for something like:
function mymodule_menu() {
$items['node/%node/nextpage'] = array(
'title' => 'Next page',
'type' => MENU_LOCAL_TASK, // Makes it a tab on node/%node-pages
'page callback' => 'mymodule_node_page_view', // Your page display function
'page arguments' => array(1), // First will be a node object, second will be whatever value is passed in the url
// You should rip access callback and access arguments from node_menu()
);
return $items;
}
That should do something like what you are asking for.
It is also possible, easier and definitely recommended to do this with Panels/Pages (see also Chaos Tools) or arguably Views as they are quite capable of all this and generally a better way to work with Drupal's strengths than custom code.
Updated
To clarify I've simplified the menu hook and you should be able to use the below page view function. I still believe you would make a better solution using Panels and overriding node_view and such.
The MENU_LOCAL_TASK part in the menu hook should turn this into another tab along with View and Edit.
function mymodule_node_page_view($node) {
die("It works: ".$node->title);
}
Hope that's more helpful.
I have a default user registration form that cannot change. However my boss wants a second registration form that is laid out differently than the first. I am new to Drupal so some explanation would be great.
Thank you in advance
If you create a custom module you can define a path for the second menu item using hook_menu().
function user_menu() {
$items['user/custom_register'] = array(
'title' => 'Create new account',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_register'),
'access callback' => 'user_register_access',
'type' => MENU_LOCAL_TASK,
'file' => 'user.pages.inc',
);
return $items;
}
Of course this will not look any different to your exsisting form, it will just be a different path.
To customize the form you have a coulpe of options, you could use hook_form_alter() and check the path. Or you could change the page arguments argument above to something which called user_register and customizes the output.
Let me save you some time, as I just solved this on a few sites. Check out the login forms here:
http://www.dogfish.com/user
http://www.inclind.com/user
This is the basic user login form. I am overriding it by telling Drupal to use a custom tpl file to load this page, and in the TPL, I am added additional elements and adding to style.css.
There is a write up on how to do that here:
http://www.lullabot.com/articles/hacking-phptemplate
Halfway down it shows you how to work with template.php to define new tpl pages for specific paths.
What you want to do is tell it when 'user' or 'user/register' is loaded, use a tpl file (you can name it). Then, you can work with the preprocessor functions and add to the page, like change the into words at the top, or remove certain form elements. The benefit is you can add all you want to the user registration form through the core Profile module or other modules, and they will still be presented here.
It will save you a lot of time this way instead of try to reinvent the user login process with your own module (I've tried that too).