Exemple #1
0
 /**
  * Saves an element's content.
  *
  * @param BaseElementModel $element            The element whose content we're saving.
  * @param bool             $validate           Whether the element's content should be validated first.
  * @param bool             $updateOtherLocales Whether any non-translatable fields' values should be copied to the
  *                                             element's other locales.
  *
  * @throws Exception
  * @return bool Whether the content was saved successfully. If it wasn't, any validation errors will be saved on the
  *              element and its content model.
  */
 public function saveContent(BaseElementModel $element, $validate = true, $updateOtherLocales = true)
 {
     if (!$element->id) {
         throw new Exception(Craft::t('Cannot save the content of an unsaved element.'));
     }
     $originalContentTable = $this->contentTable;
     $originalFieldColumnPrefix = $this->fieldColumnPrefix;
     $originalFieldContext = $this->fieldContext;
     $this->contentTable = $element->getContentTable();
     $this->fieldColumnPrefix = $element->getFieldColumnPrefix();
     $this->fieldContext = $element->getFieldContext();
     $content = $element->getContent();
     if (!$validate || $this->validateContent($element)) {
         $this->_saveContentRow($content);
         $fieldLayout = $element->getFieldLayout();
         if ($fieldLayout) {
             if ($updateOtherLocales && craft()->isLocalized()) {
                 $this->_duplicateNonTranslatableFieldValues($element, $content, $fieldLayout, $nonTranslatableFields, $otherContentModels);
             }
             $this->_updateSearchIndexes($element, $content, $fieldLayout, $nonTranslatableFields, $otherContentModels);
         }
         $success = true;
     } else {
         $element->addErrors($content->getErrors());
         $success = false;
     }
     $this->contentTable = $originalContentTable;
     $this->fieldColumnPrefix = $originalFieldColumnPrefix;
     $this->fieldContext = $originalFieldContext;
     return $success;
 }