Actions

Difference between revisions of "Add text to an article using template overrides"

From Joomla! Documentation

(new tip on adding text automatically when editing in the front end)
 
m (edit <code> tags to <pre> tags for better page layout)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
This tip tells you how to change Joomla! to automatically add text to an article. Say, for example, that you want to have some standard text ("This text has been added automatically by Joomla!.") added to the end of each article that is submitted from the front end. Here is how you can do this.
+
Automatically add text to an article in Joomla! If you want to add standard text ("This text has been added automatically by Joomla!") to the end of each article that is submitted from the front end, follow these steps:
# Create a template override for the file <code><joomla_root>/components/com_content/views/article/tmpl/form.php</code>
+
 
#* Create a folder called <code><joomla_root>/templates/<your template>/html/com_content/article<code>. For example, for the "beez" template this folder is called <code><joomla_root>/templates/beez/html/com_content/article</code>.
+
# Create a template override for the file <pre><joomla_root>/components/com_content/views/article/tmpl/form.php</pre>
#* Copy the standard <code>form.php</code> file from the components folder above to the templates folder. Note: If you already have a form.php file in this folder, don't copy the file. Just edit it as indicated below.
+
#* Create a folder called <pre><joomla_root>/templates/<your template>/html/com_content/article</pre>For example, for the "beez" template this folder is called <pre><joomla_root>/templates/beez/html/com_content/article</pre>.
 +
#* Copy the standard <pre>form.php</pre> file from the components folder above to the templates folder. Note: If you already have a form.php file in this folder, don't copy the file. Just edit it as indicated below.
 
# Edit the copy of the file you made in the override folder as follows. Find the line <source lang="javascript">
 
# Edit the copy of the file you made in the override folder as follows. Find the line <source lang="javascript">
 
   var text = <?php echo $this->editor->getContent( 'text' ); ?></source> This is line 49 in the 1.5.8 version of this file. Change this to the following:  
 
   var text = <?php echo $this->editor->getContent( 'text' ); ?></source> This is line 49 in the 1.5.8 version of this file. Change this to the following:  
Line 12: Line 13:
 
           $this->article->text .= $addToText;
 
           $this->article->text .= $addToText;
 
}
 
}
   echo $this->editor->getContent( 'text' ); ?> </source> This is checking that the text to be added is not already in the article. If it is not, then it is added to the article. Obviously, you would change the text in the variable <code>$addToText</code> to whatever you need.
+
   echo $this->editor->getContent( 'text' ); ?> </source> This is checking that the text to be added is not already in the article. If it is not, then it is added to the article.  
 +
Don't forget to change the text in the variable <code>$addToText</code> to whatever you need.
  
 
That's it. Now, when you edit an article from the front end, this text will automatically be added. Note that this change does not affect editing from the back end. Also, since this is done as a template override, it only affects pages that use the overridden template.
 
That's it. Now, when you edit an article from the front end, this text will automatically be added. Note that this change does not affect editing from the back end. Also, since this is done as a template override, it only affects pages that use the overridden template.
Line 20: Line 22:
 
[[Category:Tips and tricks]]
 
[[Category:Tips and tricks]]
 
[[Category:Tips and tricks 1.5]]
 
[[Category:Tips and tricks 1.5]]
 +
[[Category:Article Management]]
 +
[[Category:Templates]]
 +
[[Category:Template Development]]
 +
[[Category:Tutorials]]

Latest revision as of 18:12, 23 July 2012

Copyedit.png
This Article Needs Your Help

This article is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


Automatically add text to an article in Joomla! If you want to add standard text ("This text has been added automatically by Joomla!") to the end of each article that is submitted from the front end, follow these steps:

  1. Create a template override for the file
    <joomla_root>/components/com_content/views/article/tmpl/form.php
    
    • Create a folder called
      <joomla_root>/templates/<your template>/html/com_content/article
      
      For example, for the "beez" template this folder is called
      <joomla_root>/templates/beez/html/com_content/article
      
      .
    • Copy the standard
      form.php
      
      file from the components folder above to the templates folder. Note: If you already have a form.php file in this folder, don't copy the file. Just edit it as indicated below.
  2. Edit the copy of the file you made in the override folder as follows. Find the line
       var text = <?php echo $this->editor->getContent( 'text' ); ?>
    
    This is line 49 in the 1.5.8 version of this file. Change this to the following:
 
   var text = <?php 
        $addToText = '<p>Joomla! automatically added this text.</p>'; 
        if (strpos($this->article->text, $addToText) == 0) {
           $this->article->text .= $addToText;
        }
   echo $this->editor->getContent( 'text' ); ?>

This is checking that the text to be added is not already in the article. If it is not, then it is added to the article.

Don't forget to change the text in the variable $addToText to whatever you need.

That's it. Now, when you edit an article from the front end, this text will automatically be added. Note that this change does not affect editing from the back end. Also, since this is done as a template override, it only affects pages that use the overridden template.

For more information on template overrides, see the tip How to override the output from the Joomla! core.