Difference between revisions of "Plugin/Events/Content"

From Joomla! Documentation

< Plugin‎ | Events
(s/onAfterContentSave/onContentAfterSave/)
(OnContentPrepare updated)
Line 2: Line 2:
 
{{version/tutor|2.5,3.x|alt=1.5|altlink=J1.5:Plugin/Events/Content|alttitle=this guide for old event names!}}
 
{{version/tutor|2.5,3.x|alt=1.5|altlink=J1.5:Plugin/Events/Content|alttitle=this guide for old event names!}}
 
{{RightTOC}}
 
{{RightTOC}}
Content events are triggered during the content creation process. This may occur in views in the com_content component, or in other components or modules.  
+
Content events are triggered during the content creation process. This occur in many views many components and modules - it is not just for the com_content component.
  
==onPrepareContent==
+
==onContentPrepare==
 
===Description===
 
===Description===
This is the first stage in preparing content for output and is the most common point  
+
This is the first stage in preparing content for output and is the most common point for content orientated plugins to do their work. Since the article and related parameters are passed by reference, event handlers can modify them prior to display.
for content orientated plugins to do their work. Since the article and related parameters  
+
 
are passed by reference, event handlers can modify them prior to display.
 
 
===Parameters===
 
===Parameters===
*<code>article</code> A reference to the article that is being rendered by the view.
+
$context, &$row, &$params, $page = 0
*<code>params</code> A reference to an associative array of relevant parameters. The view determines what it considers to be relevant and passes that information along.
+
*<code>context</code> The context of the content being passed to the plugin - this is the component name and view - or name of module.
*<code>limitstart</code> An integer that determines the "page" of the content that is to be generated. Note that in the context of views that might not generate HTML output, a page is a reasonably abstract concept that depends on the context.
+
*<code>&article</code> A reference to the article that is being rendered by the view (For example: the text of a com_content article can be found with $article->text).
 +
*<code>&row</code> A reference to an associative array of relevant parameters. The view determines what it considers to be relevant and passes that information along.
 +
*<code>&params</code> A reference to an associative array of relevant parameters. The view determines what it considers to be relevant and passes that information along.
 +
*<code>page</code> An integer that determines the "page" of the content that is to be generated. Note that in the context of views that might not generate HTML output, a page is a reasonably abstract concept that depends on the context.
 +
 
 
===Return Value===
 
===Return Value===
None. Results are returned by modifying the referenced arguments.
+
None. Results are returned by modifying the referenced arguments. Sometimes a boolean might be returned to check for success or failure of the event.
===Used in files===
+
 
*<tt>components/com_content/views/article/view.html.php</tt>
+
===Example===
*<tt>components/com_content/views/article/view.pdf.php</tt>
+
*<tt>[[https://github.com/joomla/joomla-cms/blob/master/plugins/content/emailcloak/emailcloak.php|plugins/content/emailcloak.php]]</tt>
*<tt>components/com_content/views/category/view.html.php</tt>
 
*<tt>components/com_content/views/frontpage/view.html.php</tt>
 
*<tt>components/com_content/views/section/view.html.php</tt>
 
*<tt>plugins/content/code.php</tt>
 
*<tt>plugins/content/emailcloak.php</tt>
 
*<tt>plugins/content/geshi.php</tt>
 
*<tt>plugins/content/image.php</tt>
 
*<tt>plugins/content/loadmodule.php</tt>
 
*<tt>plugins/content/pagebreak.php</tt>
 
*<tt>plugins/content/sef.php</tt>
 
  
 
==onContentAfterTitle==
 
==onContentAfterTitle==

Revision as of 08:28, 27 August 2013

Quill icon.png
Content is Incomplete

This article or section is incomplete, which means it may be lacking information. You are welcome to assist in its completion by editing it as well. If this article or section has not been edited in several days, please consider helping complete the content.
This article was last edited by Wilsonge (talk| contribs) 10 years ago. (Purge)


Content events are triggered during the content creation process. This occur in many views many components and modules - it is not just for the com_content component.

onContentPrepare[edit]

Description[edit]

This is the first stage in preparing content for output and is the most common point for content orientated plugins to do their work. Since the article and related parameters are passed by reference, event handlers can modify them prior to display.

Parameters[edit]

$context, &$row, &$params, $page = 0

  • context The context of the content being passed to the plugin - this is the component name and view - or name of module.
  • &article A reference to the article that is being rendered by the view (For example: the text of a com_content article can be found with $article->text).
  • &row A reference to an associative array of relevant parameters. The view determines what it considers to be relevant and passes that information along.
  • &params A reference to an associative array of relevant parameters. The view determines what it considers to be relevant and passes that information along.
  • page An integer that determines the "page" of the content that is to be generated. Note that in the context of views that might not generate HTML output, a page is a reasonably abstract concept that depends on the context.

Return Value[edit]

None. Results are returned by modifying the referenced arguments. Sometimes a boolean might be returned to check for success or failure of the event.

Example[edit]

onContentAfterTitle[edit]

Description[edit]

This is a request for information that should be placed between the content title and the content body. Although parameters are passed by reference, this is not the event to modify article data. Use onPrepareContent for that purpose.

Parameters[edit]

See the onPrepareContent event for additional discussion of these parameters.

  • context The context of the content being passed to the plugin.
  • article The article that is being rendered by the view.
  • params An associative array of relevant parameters.
  • limitstart An integer that determines the "page" of the content that is to be generated.

Return Value[edit]

String. Returned value from this event will be displayed in a placeholder. Most templates display this placeholder after the article separator.

Used in files[edit]

  • components/com_content/views/article/view.html.php
  • components/com_content/views/category/view.html.php
  • components/com_content/views/frontpage/view.html.php
  • components/com_content/views/section/view.html.php
  • modules/mod_newsflash/helper.php

See Also[edit]

  • components/com_content/views/article/tmpl/default.php

onContentBeforeDisplay[edit]

Description[edit]

This is a request for information that should be placed immediately before the generated content. For views that generate XHTML/HTML, this might include the use of styles that are specified as part of the content or related parameters. Although parameters are passed by reference, this is not the event to modify article data. Use onPrepareContent for that purpose.

Parameters[edit]

See the onPrepareContent event for additional discussion of these parameters. Note that unlike onPrepareContent, these parameters are passed by value.

  • context The context of the content being passed to the plugin.
  • article The article that is being rendered by the view.
  • params An associative array of relevant parameters.
  • limitstart An integer that determines the "page" of the content that is to be generated.

Return Value[edit]

String. Returned value from this event will be displayed in a placeholder. Most templates display this placeholder after the article separator.

Used in files[edit]

  • components/com_content/views/article/view.html.php
  • components/com_content/views/category/view.html.php
  • components/com_content/views/frontpage/view.html.php
  • components/com_content/views/section/view.html.php
  • modules/mod_newsflash/helper.php

See Also[edit]

  • components/com_content/views/article/tmpl/default.php

onContentAfterDisplay[edit]

Description[edit]

This is a request for information that should be placed immediately after the generated content. For views that generate XHTML/HTML, this might include the closure of styles that are specified as part of the content or related parameters. Although parameters are passed by reference, this is not the event to modify article data. Use onPrepareContent for that purpose.

Parameters[edit]

See the onPrepareContent event for additional discussion of these parameters. Note that unlike onPrepareContent, these parameters are passed by value.

  • context The context of the content being passed to the plugin (ex. com_content.article).
  • article The article that is being rendered by the view.
  • params A JRegistry object of merged article and menu item params.
  • limitstart An integer that determines the "page" of the content that is to be generated.

Return Value[edit]

String. Returned value from this event will be displayed in a placeholder. Most templates display this placeholder after the article separator.

Example[edit]

/**
 * Display the number of hits at the end of the article.
 *
 * @param  string     $context     The context of the content being passed to the plugin.
 * @param  stdClass   $article     The article that is being rendered by the view.
 * @param  JRegistry  $params      A JRegistry object of merged article and menu item params.
 * @param  integer    $limitstart  The current page number (starting at 0).
 *
 * @return string                  The content to display after the article (or other primary content).
 */
public function onContentAfterDisplay($context, stdClass $article, JRegistry $params, $limitstart = 0)
{
	if ($context != 'com_content.article')
	{
		return '';
	}

	return 'This article has been viewed ' . $article->hits . ' times.';
}

Used in files[edit]

  • components/com_content/views/article/view.html.php
  • components/com_content/views/category/view.html.php
  • components/com_content/views/frontpage/view.html.php
  • components/com_content/views/section/view.html.php

See Also[edit]

  • components/com_content/views/article/tmpl/default.php

onBeforeContentSave[edit]

Description[edit]

This is an event that is called right before the content is saved into the database. Since the article object is passed by reference, you can make any changes you wish to the data. You can also abort the save by returning false. In this case the error set by calling $article->setError($message); will be displayed to user.

Parameters[edit]

  • article A reference to the JTableContent object that is being saved which holds the article data.
  • isNew A boolean which is set to true if the content is about to be created.

Return Value[edit]

Boolean. Result will affect the saving process. See description for details.

Used in files[edit]

  • administrator/components/com_content/controller.php
  • components/com_content/models/article.php

onContentAfterSave[edit]

Description[edit]

This is an event that is called after the content is saved into the database. Even though article object is passed by reference, changes will not be saved since storing data into database phase is past. An example use case would be redirecting user to the appropriate place after saving.

Parameters[edit]

  • article A reference to the JTableContent object that is being saved which holds the article data.
  • isNew A boolean which is set to true if the content is about to be created.

Return Value[edit]

None. Result will be omitted.

Used in files[edit]

  • administrator/components/com_content/controller.php
  • components/com_content/models/article.php

onContentPrepareData[edit]

Description[edit]

Called after the data for a JForm has been retrieved. It can be used to modify the data for a JForm object in memory before rendering.

Parameters[edit]

  • context The context where this event was triggered (for example, 'com_users.profile'). Use this to check whether you are in the desired context for the plugin.
  • data An object containing the data for the form.

Return Value[edit]

  • boolean True if method succeeds.

Used in files[edit]

  • administrator/components/com_admin/models/profile.php
  • administrator/components/com_users/models/user.php
  • components/com_contact/models/contact.php
  • components/com_users/models/profile.php
  • plugins/user/profile/profile.php

onContentPrepareForm[edit]

Description[edit]

Called before a JForm is rendered. It can be used to modify the JForm object in memory before rendering. For example, use JForm->loadFile() to add fields or JForm->removeField() to remove fields. Or use JForm->setFieldAttribute() or other JForm methods to modify fields for the form.

Parameters[edit]

  • form The JForm object to be displayed. Use the $form->getName() method to check whether this is the form you want to work with.
  • data An object containing the data for the form.

Return Value[edit]

  • boolean True if method succeeds.

Used in files[edit]

  • components/com_contact/models/contact.php
  • libraries/joomla/application/component/modelform.php
  • components/com_users/models/login.php
  • plugins/user/profile/profile.php