J1.5

Using the editor in a component

From Joomla! Documentation

Revision as of 09:29, 11 September 2008 by Intertron (talk | contribs)

The "J1.5" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Introduction[edit]

To get some interaction with the visitors of your website your visitors need to be able to add some content.

This article will show you how visitors can add some data using a rich text editor.

Lets start with the component created in the article Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface which lets the administrator add new entries to the database. The component can be downloaded at: com_hello4_01

Expand the data model[edit]

The model used in the tutorial can only hold a text of 25 characters. Lets add a new field which can be used to hold a lot more text, so we can save the editor contents.

Use your favorite tool to access the php database and open the jos_hello tabel in your joomla database. Add a new field to this table and add some intial text into it by running this SQL statement:

ALTER TABLE `jos_hello` ADD `content` TEXT NOT NULL ;
UPDATE jos_hello SET content = '<p>The A "hello world" program is a computer program that prints out "Hello, world!"
 on a display device. It is used in many introductory tutorials for teaching a programming language. Such a program is typically one
 of the simplest programs possible in a computer language. Some are surprisingly complex, especially in some graphical user interface (GUI)
 contexts, but most are very simple, especially those which rely heavily on a particular command line interpreter ("shell") to perform the
 actual output. In many embedded systems, the text may be sent to a one or two-line liquid crystal display (LCD), or some other appropriate
 signal, such as a LED being turned on, may substitute for the message.<p>' WHERE `jos_hello`.`id` = 1

Show the new field on the front end[edit]

To show the new content we must:

  • Change the model to read the field from the database
  • Change the view to pass the value of the field into the template
  • Change the template to actually show the text

The model was stored in the file: components/com_hello/models/hello.php
We must change the query to get the new field from the database. The loadResult method only retrieves the first field of the record, so we change this in loadObject to get the data into an object. Our new getGreeting() method will therefore look like:

function getGreeting()
{
	$db =& JFactory::getDBO();

	$query = 'SELECT greeting, content FROM #__hello';
	$db->setQuery( $query );
	$greeting = $db->loadObject();

	return $greeting;
}

The view was stored in the file: components/com_hello/views/hello/view.html.php
As a reminder: The view pulls the data from the model and fees the data into the template
Because the output of the model is now an object instead of just text, we change the line which assigns the greeting to the template to only assign the greeting text of the object. We add a similar line to assign the content to the template.

function display($tpl = null)
{
	$greeting = $this->get( 'Greeting' );
	$this->assignRef( 'greeting',	$greeting->greeting );
	$this->assignRef( 'content',	$greeting->content );
		
	parent::display($tpl);
}

Finally we will change the template in components/com_hello/views/hello/tmpl/default.php to become:

<?php // no direct access
defined('_JEXEC') or die('Restricted access'); ?>
<h1><?php echo $this->greeting; ?></h1>
<?php echo $this->content; ?>