FIX: minor fixtures

This commit is contained in:
Tony Air 2021-02-26 11:14:20 +07:00
parent 878feb96a3
commit b6c20dbf8c
3 changed files with 65 additions and 35 deletions

View File

@ -27,16 +27,16 @@ class ElementImageWidget extends DataExtension
];
private static $available_widths = [
'300' => 'Small (300px)',
'300' => 'Small (300px)',
'400' => 'Medium (400px)',
'600' => 'Big (600px)',
];
private static $db = [
'Resize' => 'Boolean(1)',
'ManualWidth' => 'Boolean(0)',
'ManualWidth' => 'Boolean(0)',
'ImageHeight' => 'Float',
'ImageWidth' => 'Float',
'ImageWidth' => 'Float',
'Content' => 'HTMLText',
];
@ -48,44 +48,42 @@ class ElementImageWidget extends DataExtension
{
parent::updateCMSFields($fields);
$fields->insertBefore(
'Image',
LinkField::create('ImageLinkID', 'Link')
);
$fields->removeByName(['ImageLinkID', 'Resize']);
$fields->push(LinkField::create('ImageLinkID', 'Link'));
$this->owner->ImageHeight = $this->getHeight();
$heights = Config::inst()->get(__CLASS__, 'available_heights');
$widths = Config::inst()->get(__CLASS__, 'available_widths');
$fields->replaceField('Resize', CheckboxField::create(
$fields->push(CheckboxField::create(
'Resize',
'Would you like to scale image?'
));
if (count($heights)) {
$fields->removeByName(['ManualWidth','ImageWidth',]);
$fields->replaceField(
'ImageHeight',
CompositeField::create(
DropdownField::create(
'ImageHeight',
'Image Height',
$heights,
$this->getHeight()
)
->setEmptyString('(auto)')
->displayIf('Resize')->isChecked()->end(),
CheckboxField::create('ManualWidth', 'Set Width Manually')
->displayIf('Resize')->isChecked()->end(),
DropdownField::create(
'ImageWidth',
'Image Width',
$widths
)
->setEmptyString('(auto)')
->displayIf('ManualWidth')->isChecked()->end()
)
$fields->removeByName(['ManualWidth','ImageWidth', 'ImageHeight']);
$fields->push(
CompositeField::create(
DropdownField::create(
'ImageHeight',
'Image Height',
$heights,
$this->getHeight()
)
->setEmptyString('(auto)')
->displayIf('Resize')->isChecked()->end(),
CheckboxField::create('ManualWidth', 'Set Width Manually')
->displayIf('Resize')->isChecked()->end(),
DropdownField::create(
'ImageWidth',
'Image Width',
$widths
)
->setEmptyString('(auto)')
->displayIf('ManualWidth')->isChecked()->end()
)
);
} else {
$fields->dataFieldByName('ImageHeight')
@ -117,10 +115,10 @@ class ElementImageWidget extends DataExtension
public function getWidth()
{
$obj = $this->owner;
$obj = $this->owner;
return $obj->getField('ManualWidth') && $obj->getField('ImageWidth')
? $obj->getField('ImageWidth')
: $obj->getColumnWidthRecursive();
? $obj->getField('ImageWidth')
: $obj->getColumnWidthRecursive();
}
public function getHeight()
@ -140,4 +138,15 @@ class ElementImageWidget extends DataExtension
return 0;
}
public function onBeforeWrite()
{
$title = $this->owner->getField('Title');
$img = $this->owner->Image();
if (!$title && $img) {
$this->owner->setField('Title', $img->getTitle());
}
parent::onBeforeWrite();
}
}

View File

@ -44,6 +44,27 @@ class BannerWidget extends Widget
public function getSibling()
{
return false;
return false;
}
private $_random;
public function Random()
{
if (!$this->_random) {
$this->_random = self::get()->filter('Enabled', true)->sort('RAND()')->first();
}
return $this->_random;
}
public function onBeforeWrite()
{
$title = $this->getField('Title');
$img = $this->Image();
if (!$title && $img) {
$this->setField('Title', $img->getTitle());
}
parent::onBeforeWrite();
}
}

View File

@ -2,7 +2,7 @@
<div class="image-element__image<% if $Resize %><% if $Height %> height{$Height}<% end_if %><% if $Width %> width{$Width}<% end_if %><% end_if %>">
<img
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
data-lazy-src="$ImageResized.URL" class="img-responsive" alt="$Title.ATT"
data-lazy-src="$ImageResized.URL" class="img-responsive" alt="$Title"
/>
</div>
<% end_if %>