public function prepValue($value)
{
if (!$value) {
$value = new Seomatic_MetaFieldModel();
$value->seoMainEntityCategory = $this->getSettings()->seoMainEntityCategory;
$value->seoMainEntityOfPage = $this->getSettings()->seoMainEntityOfPage;
$value->seoTitle = $this->getSettings()->seoTitle;
$value->seoTitleUnparsed = $this->getSettings()->seoTitle;
$value->seoTitleSource = $this->getSettings()->seoTitleSource;
$value->seoTitleSourceField = $this->getSettings()->seoTitleSourceField;
$value->seoDescription = $this->getSettings()->seoDescription;
$value->seoDescriptionUnparsed = $this->getSettings()->seoDescription;
$value->seoDescriptionSource = $this->getSettings()->seoDescriptionSource;
$value->seoDescriptionSourceField = $this->getSettings()->seoDescriptionSourceField;
$value->seoKeywords = $this->getSettings()->seoKeywords;
$value->seoKeywordsUnparsed = $this->getSettings()->seoKeywords;
$value->seoKeywordsSource = $this->getSettings()->seoKeywordsSource;
$value->seoKeywordsSourceField = $this->getSettings()->seoKeywordsSourceField;
$value->seoImageIdSource = $this->getSettings()->seoImageIdSource;
$value->seoImageIdSourceField = $this->getSettings()->seoImageIdSourceField;
$value->seoImageTransform = $this->getSettings()->seoImageTransform;
$value->twitterCardType = $this->getSettings()->twitterCardType;
$value->seoTwitterImageIdSource = $this->getSettings()->seoTwitterImageIdSource;
$value->seoTwitterImageIdSourceField = $this->getSettings()->seoTwitterImageIdSourceField;
$value->seoTwitterImageTransform = $this->getSettings()->seoTwitterImageTransform;
$value->openGraphType = $this->getSettings()->openGraphType;
$value->seoFacebookImageIdSource = $this->getSettings()->seoFacebookImageIdSource;
$value->seoFacebookImageIdSourceField = $this->getSettings()->seoFacebookImageIdSourceField;
$value->seoFacebookImageTransform = $this->getSettings()->seoFacebookImageTransform;
$value->robots = $this->getSettings()->robots;
}
/* -- Handle pulling values from other fields */
$element = $this->element;
if ($value->seoTitleUnparsed == "") {
$value->seoTitleUnparsed = $value->seoTitle;
}
if ($value->seoDescriptionUnparsed == "") {
$value->seoDescriptionUnparsed = $value->seoDescription;
}
if ($value->seoKeywordsUnparsed == "") {
$value->seoKeywordsUnparsed = $value->seoKeywords;
}
/* -- If we're attached to a Commerce_Product element, always have the Main Enity of Page be a Product */
$elemType = $element->getElementType();
if ($elemType == "Commerce_Product") {
$value->seoMainEntityCategory = "Product";
$value->seoMainEntityOfPage = "";
}
if ($element) {
/* -- Swap in any SEOmatic fields that are pulling from other entry fields */
switch ($value->seoTitleSource) {
case 'field':
if (isset($element[$value->seoTitleSourceField])) {
$value->seoTitle = craft()->seomatic->getTextFromEntryField($element[$value->seoTitleSourceField]);
if (craft()->config->get("truncateTitleTags", "seomatic")) {
$truncLength = craft()->config->get("maxTitleLength", "seomatic");
$value->seoTitle = craft()->seomatic->truncateStringOnWord($value->seoTitle, $truncLength);
}
}
break;
case 'custom':
$value->seoTitle = craft()->seomatic->parseAsTemplate($value->seoTitleUnparsed, $element);
break;
}
switch ($value->seoDescriptionSource) {
case 'field':
if (isset($element[$value->seoDescriptionSourceField])) {
$value->seoDescription = craft()->seomatic->getTextFromEntryField($element[$value->seoDescriptionSourceField]);
if (craft()->config->get("truncateDescriptionTags", "seomatic")) {
$truncLength = craft()->config->get("maxDescriptionLength", "seomatic");
$value->seoDescription = craft()->seomatic->truncateStringOnWord($value->seoDescription, $truncLength);
}
}
break;
case 'custom':
$value->seoDescription = craft()->seomatic->parseAsTemplate($value->seoDescriptionUnparsed, $element);
break;
}
switch ($value->seoKeywordsSource) {
case 'field':
if (isset($element[$value->seoKeywordsSourceField])) {
$value->seoKeywords = craft()->seomatic->getTextFromEntryField($element[$value->seoKeywordsSourceField]);
if (craft()->config->get("truncateKeywordsTags", "seomatic")) {
$truncLength = craft()->config->get("maxKeywordsLength", "seomatic");
$value->seoKeywords = craft()->seomatic->truncateStringOnWord($value->seoKeywords, $truncLength);
}
}
break;
case 'keywords':
if (isset($element[$value->seoKeywordsSourceField])) {
$text = craft()->seomatic->getTextFromEntryField($element[$value->seoKeywordsSourceField]);
$value->seoKeywords = craft()->seomatic->extractKeywords($text);
}
break;
case 'custom':
$value->seoKeywords = craft()->seomatic->parseAsTemplate($value->seoKeywordsUnparsed, $element);
break;
}
switch ($value->seoImageIdSource) {
case 'field':
if (isset($element[$value->seoImageIdSourceField]) && $element[$value->seoImageIdSourceField]->first()) {
$value->seoImageId = $element[$value->seoImageIdSourceField]->first()->id;
}
break;
}
switch ($value->seoTwitterImageIdSource) {
case 'field':
if (isset($element[$value->seoTwitterImageIdSourceField]) && $element[$value->seoTwitterImageIdSourceField]->first()) {
$value->seoTwitterImageId = $element[$value->seoTwitterImageIdSourceField]->first()->id;
}
break;
}
switch ($value->seoFacebookImageIdSource) {
case 'field':
if (isset($element[$value->seoFacebookImageIdSourceField]) && $element[$value->seoFacebookImageIdSourceField]->first()) {
$value->seoFacebookImageId = $element[$value->seoFacebookImageIdSourceField]->first()->id;
}
break;
}
}
if (craft()->request->isSiteRequest()) {
}
return $value;
}