Source of file Extension.php
Size: 4,539 Bytes - Last Modified: 2021-12-23T10:51:09+00:00
/var/www/docs.ssmods.com/process/src/src/Extension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 | <?php namespace Terraformers\ScaffoldedFields; use SilverStripe\Forms\FieldList; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataObject; /** * Class Extension * * Removal of scaffolded fields * this is useful for project setup where scaffolded fields are not needed * there are various options on how to remove scaffolded fields * * Note that this extension needs to be applied manually and order matters * you want to apply this extension after all scaffolded fields are present not before your model level * fields customisation * * example config * * DNADesign\Elemental\Models\BaseElement: * extensions: * - Terraformers\ScaffoldedFields\Extension * field_removal: * - # remove all db fields except Title and ShowTitle * property: db * type: keep * fields: * Title * ShowTitle * - # remove all has_one fields * property: has_one * type: keep * - # remove LinkTracking, FileTracking and BackLinkTracking fields from many_many * property: many_many * type: remove * fields: * LinkTracking * FileTracking * BackLinkTracking * - # remove Settings field (not part of any static property) * property: extra * fields: * Settings * * @property DataObject $owner * @package Terraformers\ScaffoldedFields */ class Extension extends DataExtension { const TYPE_KEEP = 'keep'; const TYPE_REMOVE = 'remove'; /** * Specify which scaffolded fields need to be kept and which should be removed * configuration is a collection of rules * each rules has consist of the following * - property (db, has_one, has_many, many_many or extra) * - type (keep or remove), this is not required when extra property is used * - fields list of field names that the configuration applies to * * @config * @var array */ private static $field_removal = []; /** * Extension point in @see DataObject::getCMSFields() * * @param FieldList $fields */ public function updateCMSFields(FieldList $fields) { $this->removeFields($fields); } /** * @param FieldList $fields */ private function removeFields(FieldList $fields) { $rules = $this->owner->config()->get('field_removal'); if (!is_array($rules)) { return; } foreach ($rules as $rule) { if (!$this->validateRule($rule)) { continue; } $property = $rule['property']; $list = array_key_exists('fields', $rule) ? $rule['fields'] : []; $list = is_array($list) ? $list : []; if ($property === 'extra' && count($list) > 0) { $fields->removeByName($list); continue; } $type = $rule['type']; $this->removeDbFields($property, $type, $list, $fields); } } /** * @param $property * @param $type * @param array $list * @param FieldList $fields */ private function removeDbFields($property, $type, array $list, FieldList $fields) { $config = $this->owner->config()->get($property); if (!is_array($config)) { return; } $fieldsToRemove = array_keys($config); foreach ($fieldsToRemove as $fieldName) { $active = in_array($fieldName, $list); $keep = $type === self::TYPE_KEEP && $active || $type === self::TYPE_REMOVE && !$active; if ($keep) { continue; } if ($property === 'has_one') { $fields->removeByName($fieldName . 'ID'); } $fields->removeByName($fieldName); } } /** * @param mixed $rule * @return bool */ private function validateRule($rule) { if (!is_array($rule)) { return false; } if (!array_key_exists('property', $rule)) { return false; } $property = $rule['property']; if ($property === 'extra') { return true; } if (!in_array($property, ['db', 'has_one', 'has_many', 'many_many'])) { return false; } if (!in_array($rule['type'], [self::TYPE_KEEP, self::TYPE_REMOVE])) { return false; } return true; } } |