Actions

J1.5

Difference between revisions of "Using the editor in a component"

From Joomla! Documentation

(New page: == Introduction == 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 ...)
 
Line 12: Line 12:
 
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.
 
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 by running this SQL statement:
+
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:
  
<source lang="sql">ALTER TABLE `jos_hello` ADD `content` TEXT NOT NULL ;</source>
+
<source lang="sql">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</source>
 +
 
 +
== Show the new field on the front end ==
 +
 
 +
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<br />
 +
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:
 +
 +
<source lang="php">function getGreeting()
 +
{
 +
$db =& JFactory::getDBO();
 +
 
 +
$query = 'SELECT greeting, content FROM #__hello';
 +
$db->setQuery( $query );
 +
$greeting = $db->loadObject();
 +
 
 +
return $greeting;
 +
}</source>
 +
 
 +
The view was stored in the file: components/com_hello/views/hello/view.html.php<br />
 +
As a reminder: The view pulls the data from the model and fees the data into the template<br />
 +
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.
 +
<source lang="php">function display($tpl = null)
 +
{
 +
$greeting = $this->get( 'Greeting' );
 +
$this->assignRef( 'greeting', $greeting->greeting );
 +
$this->assignRef( 'content', $greeting->content );
 +
 +
parent::display($tpl);
 +
}</source>
 +
 
 +
Finally we will change the template in components/com_hello/views/hello/tmpl/default.php to become:
 +
<source lang="php"><?php // no direct access
 +
defined('_JEXEC') or die('Restricted access'); ?>
 +
<h1><?php echo $this->greeting; ?></h1>
 +
<?php echo $this->content; ?></source>

Revision as of 09:29, 11 September 2008

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Introduction

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

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

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; ?>