Overriding Drupal 7 Theme functions - function

I am new to Drupal as well as Drupal theme development. After learning how to override drupal template, I wanted to know how to override drupal 7 theme function. From the node Using the theme layer, the following things I have learned :
For every hook in a module, we need to register a hook function
Im plementation is 'template' file if it is defined in return array of the function. Otherwiaw implementation is through function.
Now following the book A definative guide to Drupal 7, to override a theme function
1. Copy - paste the theme function to my theme's template.php file
2. Change the beginning of filename from theme_ to yourtheme_
3. Save
To give example, it overrides the following function :
function theme_more_link($variables) {
return '<div class="more-link">' . l(t('More'), $variables['url'], array('attributes' => array('title' => $variables['title']))) . '</div>';
}
Now if I want to override the theme function comment_help() in comment.module:
function comment_help($path, $arg) {
switch ($path) {
case 'admin/help#comment':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Comment module allows users to comment on site content, set commenting defaults and permissions, and moderate comments. For more information, see the online handbook entry for Comment module.', array('#comment' => 'http://drupal.org/documentation/modules/comment/')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Default and custom settings') . '</dt>';
$output .= '<dd>' . t("Each <a href='#content-type'>content type</a> can have its own default comment settings configured as: <em>Open</em> to allow new comments, <em>Hidden</em> to hide existing comments and prevent new comments, or <em>Closed</em> to view existing comments, but prevent new comments. These defaults will apply to all new content created (changes to the settings on existing content must be done manually). Other comment settings can also be customized per content type, and can be overridden for any given item of content. When a comment has no replies, it remains editable by its author, as long as the author has a user account and is logged in.", array('#content-type' => url('admin/structure/types'))) . '</dd>';
$output .= '<dt>' . t('Comment approval') . '</dt>';
$output .= '<dd>' . t("Comments from users who have the <em>Skip comment approval</em> permission are published immediately. All other comments are placed in the <a href='#comment-approval'>Unapproved comments</a> queue, until a user who has permission to <em>Administer comments</em> publishes or deletes them. Published comments can be bulk managed on the <a href='#admin-comment'>Published comments</a> administration page.", array('#comment-approval' => url('admin/content/comment/approval'), '#admin-comment' => url('admin/content/comment'))) . '</dd>';
$output .= '</dl>';
return $output;
}
}
How can I do it? Its name does not starts from theme_.

The core comment module is uging hook_help function. You have to use the same function in your custom module. Notice that you should first clean any $output data using $output = "";

Related

Add default code in wordpress post

I want to add a simple code to all new post by default
I tried to used this code in .function.php (wpbeginner)
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "If you like this post, then please consider retweeting it or sharing it on Facebook.";
return $content;
}
I am using plugin named Shortcodes Ultimate
Code:
[su_spoiler title="Download The File" style="fancy" icon="chevron-circle"]Here[/su_spoiler]
I tried to normal use simple html and CSS code but it show the same error for both cases
https://i.stack.imgur.com/wgAs4.png
Try this -: wrap your code is a single quote as you are already using double quotes.
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = '[su_spoiler title="Download The File" style="fancy" icon="chevron-circle"]Here[/su_spoiler]';
return $content;
}

Yii 2 use same controller method / action in multiple controllers

I have a method checking whether user is logged in in my UserController.I need the same method in all the other controllers.How can I do the same without copy pasting the code to all controllers.
The controller method looks like
public function is_logged_in() {
$session = Yii::$app->session;
$cookies = Yii::$app->request->cookies;
//print_r($session);
$session->open();
$session_cookie_name = Yii::$app->params['cookie_name_session_var'];
$logged_in = false;
//echo "-memn-".$cook_name.' is halle - ';
//print_r($_SESSION);
if(($cook_name = $session->get($session_cookie_name))) {
//echo " - <pre>";
//print_r($cookies);
//exit;
$write_cookies = Yii::$app->response->cookies;
//echo "</pre>";
//echo $cookies->getValue($cook_name).' placenta';
if($u_token = $cookies->getValue($cook_name)) {
echo "b";
if($u_token) {
echo "c";
$write_cookies->remove($cook_name);
unset($write_cookies[$cook_name]);
$session->destroy();
$session->open();
$cookie_name = sha1($u_token).time();
$session[$session_cookie_name] = $cookie_name;
$write_cookies->add(new \yii\web\Cookie([
'name' => $session[$session_cookie_name],
'value' => $u_token,
'expire' => time() + 6000000
])); // around one hour expiry time
$session->close();
$logged_in = true;
//echo $u_token;
}
}
}
if(!$logged_in) {
$session->destroy();
}
return $logged_in;
}
1) You can create own component and put this method here or place it in the model (depends on logic of that method). Component can be placed for example in components folder (by default it doesn't exist). Then just use this component in any controllers you want.
2) If this code needs to be executed before or after certain actions, you can use behaviors.
3) You can use inheritance and create your custom controller that extends from yii\web\Controller, declare this method here and extend all other controllers where are you going to use this logic from your custom one.
In addition to arogachev's answer your code actually should sit in a class that extends the User component class http://www.yiiframework.com/doc-2.0/yii-web-user.html , not even to mention that the user identity class already does everything your code does (only much, much better). It comes with isGuest function.

Registering custom WordPress widgets

I have created a custom widget in my themes plugin directory and it seems to be working as expected but when I register a second custom widget the 1st one seems to get overridden and I no longer have access to it. Below is my code for the widgets:
add_action( 'widgets_init', create_function( '', 'register_widget( "staffWidget" );' ) );
class staffWidget extends WP_Widget{
function staffWidget() {
parent::WP_Widget(true, 'Staff');
}
function widget($args, $instance){
echo "test widget";
}
function update($new_instance, $old_instance){
return $new_instance;
}
function form($instance){
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
if($instance['title']){
$title = $instance['title'];
}
else{
$title = "Add title here";
}
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<?php
}
}
Both widgets have this kind of code structure but with different class names and both widgets have been activated in the plugins section of the WP dashboard. Any help or suggestions would be very much appreciated. Thanks in advance :)
You are calling the class WP_Widget with the wrong parameters.
/**
* PHP5 constructor
*
* #param string $id_base Optional Base ID for the widget, lower case,
* if left empty a portion of the widget's class name will be used. Has to be unique.
* #param string $name Name for the widget displayed on the configuration page.
* #param array $widget_options Optional Passed to wp_register_sidebar_widget()
* - description: shown on the configuration page
* - classname
* #param array $control_options Optional Passed to wp_register_widget_control()
* - width: required if more than 250px
* - height: currently not used but may be needed in the future
*/
function __construct( $id_base = false, $name, $widget_options = array(), $control_options = array() ) {
If you put false (default value) or a string, it'll work. So, supposing we have a widget Staff and another Stuff, this would do:
parent::WP_Widget('staff', 'Staff', array(), array() );
parent::WP_Widget('stuff', 'Stuff', array(), array() );
Your code is using attribute_escape, which is deprecated. If you enable WP_DEBUG, you'll see the warning. Anyway, it's a good practice to develop always with it turned on.
All this indicates that you are using a bad source as example. This one is the article about custom widgets.

Missing image in cakephp blog post

I am new to cakePHP and I am tring the blog example of cakePHP 1.3 book .
I correctly upload image in this blog example.The image name in database and image in DOCUMENT_ROOT/....correctly
but now I am wanted to show image in my blog with related post.
I am using this code for image upload...
function add() {
if (!empty($this->data)) {
if(isset($this->data["Image"]["image"]["name"])){
$file = new File($this->data["Image"]["image"]["name"]);
$ext = $file->ext();
if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png') {
$this->Session->setFlash('You may only upload image files.');
}else{
if(move_uploaded_file($this->data["Image"]["image"] ["tmp_name"],$_SERVER["DOCUMENT_ROOT"]."test_om/blog/app/webroot/img/upload_image/"
. $this->data["Image"]["image"]["name"]) == true){
$this->data["Post"]["image"] = $this->data["Image"]["image"]["name"];
}
$this->Post->save($this->data);
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
}
and i am showing image form this code
<?php echo $this->Html->image('/img/upload_image/1.gif'); ?>
and this show same image with all post.
but i am wanted to set specfic image with its related post....
If you are sure you are getting everything correct (in the database and the file where it should be) you should use something like this in the view.
<?php echo $this->Html->image($this->data['Post']['image']); ?>
this is assuming you are passing the data from the controller in the way described in the tutorial to a view view :)
if is an index view you should have a variable posts that have all post info, and in the view you will be in a loop like a foreach ($post as $post). Assuming this your view should have something like this:
<?php echo $this->Html->image($post['Post']['image']); ?>
Suggestion: use debug kit (cakephp plugin) so you can see what variables are passed down and the structure (like a pr($variable))
Hope all this helps you, if not, comment this post so i can try to extend my answer if needed

drupal Webform HTML Email hooks

I'm trying to send a thank you email to the user submitting the form in HTML.
I found out by using a hook in my template.php file like this works to set the header correctly:
function mythemename_webform_mail_headers($form_values, $node, $sid) {
$headers = array(
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'X-Mailer' => 'Drupal Webform (PHP/'. phpversion() .')',
);
return $headers;
}
This works freat for the "Thank You" email.
The email that the site admin gets with the form results is also html, but it's not converting newlines to breaks in this email. I can't figure out how to use a hook for this, so I had to edit the webform.module file and do this:
function webform_mail($key, &$message, $params) {
$message['headers'] = array_merge($message['headers'], $params['headers']);
$message['subject'] = $params['subject'];
//$message['body'][] = drupal_wrap_mail($params['message']); // replaced this with line below
$message['body'][] = nl2br(drupal_wrap_mail($params['message']));
}
Can this be done with a hook in template.php?
You can use hook_mail_alter to edit mails created with hook_mail, which is what webform uses.
You can't use hook_mail_alter() in a theme, only in a custom module.
Old topic but still usefull I guess. On the webform module's edit page there is a option/fieldset with additional processing:
<?php
$to = $form_values['submitted_tree']['uw_gegevens']['e_mail'];
$from = "no-reply#example.com";
$achternaam = $form_values['submitted_tree']['uw_gegevens']['uw_naam'];
$message = drupal_mail('webform_extra', 'reply', $to, language_default(), array('body' => $body), $from, TRUE);
function webform_extra_mail($key, &$message, $params) {
$message['subject'] = "TEXT.";
$message['body'] = "
TEXT, " . $params['achternaam'] . "
TEXT.
KIND REGARDS,
TEXT
";
} ?>
Hope this helps
Guus van de Wal