Source of file ShareCareFields.php
Size: 5,643 Bytes - Last Modified: 2021-12-24T05:20:21+00:00
/var/www/docs.ssmods.com/process/src/src/ShareCareFields.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 | <?php namespace JonoM\ShareCare; use JonoM\ShareCare\ShareCare; use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\Assets\Image; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\TextareaField; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataExtension; /** * Provide default fields and method customisations to complement Open Graph * module with minimal setup. */ class ShareCareFields extends DataExtension { private static $db = array( 'OGTitleCustom' => 'Varchar(100)', 'OGDescriptionCustom' => 'Varchar(150)', ); private static $has_one = array( 'OGImageCustom' => Image::class, 'PinterestImageCustom' => Image::class, ); private static $owns = [ 'OGImageCustom', 'PinterestImageCustom' ]; /** * Add CMS fields to allow setting of custom open graph values. */ public function updateCMSFields(FieldList $fields) { $msg = _t('JonoM\ShareCare\ShareCareFields.CMSMessage', 'The preview is automatically generated from your content. You can override the default values using these fields:'); $tab = 'Root.' . _t('JonoM\ShareCare\ShareCare.TabName', 'Share'); if ($msg) { $fields->addFieldToTab($tab, new LiteralField('ShareCareFieldsMessage', '<div class="message notice"><p>' . $msg . '</p></div>')); } $fields->addFieldToTab($tab, TextField::create('OGTitleCustom', _t('JonoM\ShareCare\ShareCareFields.ShareTitle', 'Share title')) ->setAttribute('placeholder', $this->owner->getDefaultOGTitle()) ->setMaxLength(90)); $fields->addFieldToTab($tab, TextAreaField::create('OGDescriptionCustom', _t('JonoM\ShareCare\ShareCareFields.ShareDescription', 'Share description')) ->setAttribute('placeholder', $this->owner->getDefaultOGDescription()) ->setRows(2)); $fields->addFieldToTab($tab, UploadField::create('OGImageCustom', _t('JonoM\ShareCare\ShareCareFields.ShareImage', 'Share image')) ->setAllowedFileCategories('image') ->setAllowedMaxFileNumber(1) ->setDescription(_t('JonoM\ShareCare\ShareCareFields.ShareImageRatio', '{Link}Optimum image ratio</a> is 1.91:1. (1200px wide by 630px tall or better)', array('Link' => '<a href="https://developers.facebook.com/docs/sharing/best-practices#images" target="_blank">')))); if (ShareCare::config()->get('pinterest')) { $fields->addFieldToTab($tab, UploadField::create('PinterestImageCustom', _t('ShareCareFields.PinterestImage', 'Pinterest image')) ->setAllowedFileCategories('image') ->setAllowedMaxFileNumber(1) ->setDescription(_t('JonoM\ShareCare\ShareCareFields.PinterestImageDescription', 'Square/portrait or taller images look best on Pinterest. This image should be at least 750px wide.'))); } } /** * The title that will be used in the 'og:title' open graph tag. * Use a custom value if set, or fallback to a default value. * * @return string */ public function getOGTitle() { return ($this->owner->OGTitleCustom) ? $this->owner->OGTitleCustom : $this->owner->getDefaultOGTitle(); } /** * The description that will be used in the 'og:description' open graph tag. * Use a custom value if set, or fallback to a default value. * * @return string */ public function getOGDescription() { // Use OG Description if set if ($this->owner->OGDescriptionCustom) { $description = trim($this->owner->OGDescriptionCustom); if (!empty($description)) { return $description; } } return $this->owner->getDefaultOGDescription(); } /** * The default/fallback value to be used in the 'og:description' open graph tag. * * @return string */ public function getDefaultOGDescription() { // Use MetaDescription if set if ($this->owner->MetaDescription) { $description = trim($this->owner->MetaDescription); if (!empty($description)) { return $description; } } // Fall back to Content if ($this->owner->Content) { $description = trim($this->owner->obj('Content')->Summary(20, 5)); if (!empty($description)) { return $description; } } return false; } /** * The Image object or absolute URL that will be used for the 'og:image' open graph tag. * Use a custom selection if set, or fallback to a default value. * Image size specs: https://developers.facebook.com/docs/sharing/best-practices#images. * * @return Image|string|false */ public function getOGImage() { $ogImage = $this->owner->OGImageCustom(); if ($ogImage->exists()) { return ($ogImage->getWidth() > 1200) ? $ogImage->scaleWidth(1200) : $ogImage; } return $this->owner->getDefaultOGImage(); } /** * Get an Image object to be used in the 'Pin it' ($PinterestShareLink) link. * Image size specs: https://developers.pinterest.com/pin_it/. * * @return Image|null */ public function getPinterestImage() { $pinImage = $this->owner->PinterestImageCustom(); if ($pinImage->exists()) { return ($pinImage->getWidth() > 1200) ? $pinImage->scaleWidth(1200) : $pinImage; } return $this->owner->getOGImage(); } } |