Automatic Handling of Fields
Values of some fields in the form can be automatically handled. There is no need to fill in the values while creating or editing a record.
For example, alias can be generated from the title, dates can be set to the current date or null, user id can be obtained from current logged in user. Further may you also need to check validity of some fields like title should not be empty, alias should be unique.
The data submitted by the form needs to be modified before saving. This can be done at various places:
- in the Model class by overriding the save() method or preparetable() method.
- in the Table class by overriding the bind(), check(), or store() methods.
1. Model File
admin/src/Model/PlanetModel.php
public function save($data)
{
/* Add code to modify data before saving */
return parent::save($data);
}
It is better to perform automatic handling of fields in the Table class as the data can be saved not ony from administration, but also from frontend, API or by any other means.
Generating Alias
Alias is generated from the title using OutputFilter Class method.
if (empty($data['alias']))
{
if (Factory::getConfig()->get('unicodeslugs') == 1)
{
$data['alias'] = OutputFilter::stringURLUnicodeSlug($data['title']);
}
else
{
$data['alias'] = OutputFilter::stringURLSafe($data['title']);
}
}
Ordering
The ordering value is calculated by finding the maximum value in the column and then incrementing it.
if (!$data['ordering'])
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('MAX(ordering)')
->from('#__planets');
$db->setQuery($query);
$max = $db->loadResult();
$data['ordering'] = $max + 1;
}
2. bind()
The bind() splits the article text or description into intro text and full text based on read more tag in the content. This method also converts fields data from Arrays to JSON for saving into the database.
public function bind($array, $ignore = '')
{
if (isset($array['attribs']) && \is_array($array['attribs']))
{
$registry = new Registry($array['attribs']);
$array['attribs'] = (string) $registry;
}
return parent::bind($array, $ignore);
}
3. check()
The check() checks whether title of the article is not empty. This method also sets the alias, hits, publishing dates.
public function check()
{
try
{
parent::check();
}
catch (\Exception $e)
{
$this->setError($e->getMessage());
return false;
}
if (trim($this->title) == '')
{
$this->setError('Title (title) is not set.');
return false;
}
if (trim($this->alias) == '')
{
$this->alias = $this->title;
}
$this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);
// Ensure any new items have compulsory fields set
if (!$this->id)
{
// Hits must be zero on a new item
$this->hits = 0;
}
// Set publish_up to null if not set
if (!$this->publish_up)
{
$this->publish_up = null;
}
// Set publish_down to null if not set
if (!$this->publish_down)
{
$this->publish_down = null;
}
// Check the publish down date is not earlier than publish up.
if (!is_null($this->publish_up) && !is_null($this->publish_down) && $this->publish_down < $this->publish_up)
{
// Swap the dates
$temp = $this->publish_up;
$this->publish_up = $this->publish_down;
$this->publish_down = $temp;
}
return true;
}
4. store()
The store() sets created and modified dates, created by and modified by users, and also checks for unique alias.
public function store($updateNulls = true)
{
$app = Factory::getApplication();
$date = Factory::getDate()->toSql();
$user = Factory::getUser();
if (!$this->created)
{
$this->created = $date;
}
if (!$this->created_by)
{
$this->created_by = $user->get('id');
}
if ($this->id)
{
// Existing item
$this->modified_by = $user->get('id');
$this->modified = $date;
}
else
{
// Set modified to created date if not set
if (!$this->modified)
{
$this->modified = $this->created;
}
// Set modified_by to created_by user if not set
if (empty($this->modified_by))
{
$this->modified_by = $this->created_by;
}
}
// Verify that the alias is unique
$table = $app->bootComponent('com_stars')->getMVCFactory()->createTable('Planet', 'Administrator');
if ($table->load(['alias' => $this->alias]) && ($table->id != $this->id || $this->id == 0))
{
$this->setError('Alias is not unique.');
if ($table->state == -2)
{
$this->setError('Alias is not unique. The item is in Trash.');
}
return false;
}
return parent::store($updateNulls);
}