/** * Method to update one item * * @return JContent A JContent object * * @since 1.0 * @throws UnexpectedValueException */ public function createItem() { // Get the content type. $contentType = $this->state->get('content.type'); // Check if the content type is set. if (empty($contentType)) { throw new UnexpectedValueException(sprintf('%s->createItem() could not find the content type.', get_class($this))); } // Get new content $content = $this->factory->getContent($contentType); // Get fields for new content and check them $fields = $this->state->get('content.fields'); if (empty($fields)) { throw new UnexpectedValueException('Missing fields for new object'); } // Get each field for the new content $fieldsArray = preg_split('#[\\s,]+#', $fields, null, PREG_SPLIT_NO_EMPTY); foreach ($fieldsArray as $key => $fieldName) { $field = null; if ($this->state->exists('fields.' . $fieldName)) { $field = $this->state->get('fields.' . $fieldName); } if ($field == null) { throw new UnexpectedValueException('Missing field ' . $fieldName); } $content->__set($fieldName, $field); } // Create content $content = $content->create(); $content->temporary = 0; $content->update(); return $content; }
/** * Method to copy the content object. * * @return JContent The content object copy. * * @since 12.1 * @throws LogicException * @throws RuntimeException */ public function copy() { // Assert the object is loaded. $this->assertIsLoaded(); // Create a new content item. $copy = $this->factory->getContent($this->type->alias)->create(); // Get the data from this item. $data = (array) $this->dump(); // Remove unique values. unset($data['content_id']); unset($data['featured']); unset($data['created_date']); unset($data['created_user_id']); unset($data['likes']); unset($data['revision']); // Update the content title and alias. $data['title'] = JString::increment($data['title']); $data['alias'] = JFilterOutput::stringURLSafe($data['title']); // Update the copy data. $copy->bind($data); return $copy; }