/**
* Create any new tags by looking for #new# in the strings
*
* @param array $tags Tags text array from the field
*
* @return mixed If successful, metadata with new tag titles replaced by tag ids. Otherwise false.
*
* @since 3.1
*/
public function createTagsFromField($tags)
{
if (empty($tags) || $tags[0] == '') {
return;
} else {
// We will use the tags table to store them
$tagTable = Factory::getApplication()->bootComponent('com_tags')->getMVCFactory()->createTable('Tag', 'Administrator');
$newTags = array();
$canCreate = Factory::getUser()->authorise('core.create', 'com_tags');
foreach ($tags as $key => $tag) {
// User is not allowed to create tags, so don't create.
if (!$canCreate && strpos($tag, '#new#') !== false) {
continue;
}
// Remove the #new# prefix that identifies new tags
$tagText = str_replace('#new#', '', $tag);
if ($tagText === $tag) {
$newTags[] = (int) $tag;
} else {
// Clear old data if exist
$tagTable->reset();
// Try to load the selected tag
if ($tagTable->load(array('title' => $tagText))) {
$newTags[] = (int) $tagTable->id;
} else {
// Prepare tag data
$tagTable->id = 0;
$tagTable->title = $tagText;
$tagTable->published = 1;
$tagTable->description = '';
// $tagTable->language = property_exists ($item, 'language') ? $item->language : '*';
$tagTable->language = '*';
$tagTable->access = 1;
// Make this item a child of the root tag
$tagTable->setLocation($tagTable->getRootId(), 'last-child');
// Try to store tag
if ($tagTable->check()) {
// Assign the alias as path (autogenerated tags have always level 1)
$tagTable->path = $tagTable->alias;
if ($tagTable->store()) {
$newTags[] = (int) $tagTable->id;
}
}
}
}
}
// At this point $tags is an array of all tag ids
$this->tags = $newTags;
$result = $newTags;
}
return $result;
}