public function onAfterWrite()
 {
     parent::onAfterWrite();
     if ($this()->hasExtension(HasBlocks::class_name())) {
         /** @var \ManyManyList $existing */
         $existing = $this()->{HasBlocks::relationship_name()}();
         if ($this()->{self::SingleFieldName} || $this()->WasNew) {
             if ($defaultBlockClasses = $this->getDefaultBlockClasses()) {
                 // get class names along with count of each expected
                 $expected = array_count_values($defaultBlockClasses);
                 $sort = $existing->count() + 1;
                 foreach ($expected as $blockClass => $expectedCount) {
                     if (!\ClassInfo::exists($blockClass)) {
                         continue;
                     }
                     $existingCount = $existing->filter('ClassName', $blockClass)->count();
                     if ($existingCount < $expectedCount) {
                         for ($i = $existingCount; $i < $expectedCount; $i++) {
                             // generate a default title for the block from lang
                             // e.g. ContentBlock.DefaultTitle
                             $templateVars = ['pagetitle' => $this()->{Title::SingleFieldName}, 'singular' => singleton($blockClass)->i18n_singular_name(), 'index' => $i + 1];
                             // try the block class.DefaultTitle and then Block.DefaultTitle
                             $title = _t("{$blockClass}.DefaultTitle", _t('Block.DefaultTitle', '{pagetitle} {singular} - {index}', $templateVars), $templateVars);
                             /** @var Block $block */
                             $block = new $blockClass();
                             $block->update(['Title' => $title]);
                             $block->write();
                             $existing->add($block, ['Sort' => $sort++]);
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Don't show TaxonomyTerms with code of SYSTM unless you're an Admin or config.augment_enabled = false
  * @param \SQLQuery $query
  */
 public function augmentSQL(SQLQuery &$query)
 {
     parent::augmentSQL($query);
     if (static::augment_enabled() && !Permission::check('ADMIN')) {
         $query->addWhere(self::CodeFieldName . " != '" . self::SystemCode . "'");
     }
 }
 /**
  * Skip validation for Pages if not already saved so we can create new pages with DateFields as CMS saves early.
  *
  * @param \ValidationResult $result
  * @throws \ValidationException
  * @return null
  */
 public function validate(\ValidationResult $result)
 {
     if ($this() instanceof \SiteTree) {
         if (!$this()->isInDB()) {
             return null;
         }
     }
     parent::validate($result);
 }
 /**
  * Add implementors token which is csv of 'nice' names of implementors of HasRelatedPages relationship.
  * @return mixed
  */
 public function fieldDecorationTokens()
 {
     $implementors = HasRelatedPages::implementors();
     $titles = [];
     /** @var \Page $page */
     $page = $this();
     foreach ($implementors as $className => $title) {
         $relationshipName = $className::relationship_name();
         if ($page->hasRelationship($relationshipName)) {
             $titles[] = $title;
         }
     }
     return array_merge(parent::fieldDecorationTokens(), ['implementors' => implode(', ', $titles ?: ['None found on this page type '])]);
 }
 /**
  * If GridList also has another way to provide mode, then set the field to that mode if it is set and don't
  * let it change.
  */
 public function cmsFields()
 {
     $fields = parent::cmsFields();
     // has the mode been provided some other way?
     $data = [];
     $source = get_class($this);
     if ($otherWays = array_filter($this()->extend('provideGridListTemplateData', $data, $source))) {
         foreach ($otherWays as $otherWay) {
             if (isset($otherWay['Mode'])) {
                 // replace the field with a read-only field set to the first other mode found
                 $fields[static::SingleFieldName] = new \ReadonlyField(static::SingleFieldName, null, $otherWay['Mode']);
             }
         }
     }
     return $fields;
 }
 /**
  * Add has_one relationships to related class.
  *
  * @param null $class
  * @param null $extension
  * @return mixed
  */
 public function extraStatics($class = null, $extension = null)
 {
     return array_merge_recursive(parent::extraStatics($class, $extension) ?: [], ['has_one' => [static::relationship_name() => static::related_class_name()]]);
 }
 /**
  * Return static db enum schema definition for the Media and ExternalLink Option constants.
  *
  * @param null $class
  * @param null $extension
  * @return array
  */
 public function extraStatics($class = null, $extension = null)
 {
     $values = implode(',', $this->config()->get('enum_values'));
     return array_merge_recursive(parent::extraStatics($class, $extension) ?: [], ['db' => [self::MediaLinkTypeFieldName => 'enum("' . $values . '")']]);
 }
 /**
  * Handles attaching images to the saved block after it has been written if images are allowed.
  */
 public function onAfterWrite()
 {
     parent::onAfterWrite();
     /** @var Block $block */
     if ($block = $this()->Block()) {
         $request = Controller::curr()->getRequest();
         if ($imageIDs = $request->postVar('Images') ? $request->postVar('Images')['Files'] : []) {
             // TODO may need to fix so handles more than one image (later)
             if ($block->hasMethod('Image')) {
                 $block->ImageID = reset($imageIDs);
                 $block->write();
             }
         }
     }
 }
 /**
  * If the ModelTag field is empty (length 0) then fill it with generated value.
  */
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     if (0 === strlen($this()->{static::SingleFieldName})) {
         $this()->{static::SingleFieldName} = $this->generateValue();
     }
 }
 public function updateCMSFields(FieldList $fields)
 {
     $fields->removeByName(self::InternalLinkRelationship . 'ID');
     parent::updateCMSFields($fields);
     // TODO: Change the autogenerated stub
 }
 public function cmsFields()
 {
     return array_merge(parent::cmsFields(), [static::SingleFieldName => new \OptionsetField(static::SingleFieldName, '', $this->config()->get('length_map'), $this()->{static::SingleFieldName} ?: $this->defaultPageLength())]);
 }
 /**
  * Add the model tag of the current page so can use in decoration for enable/disable field.
  *
  * @return mixed
  */
 public function fieldDecorationTokens()
 {
     return array_merge(parent::fieldDecorationTokens(), ['modelTag' => \Director::get_current_page()->{ModelTag::SingleFieldName}]);
 }