/** * @inheritdoc */ protected function defineRules(): array { $rules = parent::defineRules(); $rules[] = [['id', 'fieldLayoutId'], 'number', 'integerOnly' => true]; $rules[] = [['name', 'handle'], 'trim']; $rules[] = [['name', 'handle'], 'required']; $rules[] = [['name', 'handle'], 'string', 'max' => 255]; $rules[] = [ ['handle'], HandleValidator::class, 'reservedWords' => ['id', 'dateCreated', 'dateUpdated', 'uid', 'title'], ]; if ($this->validateHandleUniqueness) { $rules[] = [ ['handle'], UniqueValidator::class, 'targetClass' => EntryTypeRecord::class, 'targetAttribute' => 'handle', 'message' => Craft::t('yii', '{attribute} "{value}" has already been taken.'), ]; } $rules[] = [['fieldLayout'], 'validateFieldLayout']; return $rules; } /** * Validates the field layout. * * @since 3.7.0 */ public function validateFieldLayout(): void { $fieldLayout = $this->getFieldLayout(); $fieldLayout->reservedFieldHandles = [ 'author', 'authorId', 'authorIds', 'authors', 'section', 'sectionId', 'type', 'postDate', ]; if (!$fieldLayout->validate()) { $this->addModelErrors($fieldLayout, 'fieldLayout'); } } /** * Use the handle as the string representation. * * @return string */ public function __toString(): string { return (string)$this->handle ?: static::class; } /** * @inheritdoc */ public function getHandle(): ?string { return $this->handle; } /** * @inheritdoc */ public function getFieldLayout(): FieldLayout { /** @var FieldLayoutBehavior $behavior */ $behavior = $this->getBehavior('fieldLayout'); return $behavior->getFieldLayout(); } /** * @inheritdoc * @since 3.3.0 */ public function getFieldContext(): string { return 'global'; } /** * @inheritdoc * @since 3.3.0 */ public function getEagerLoadingPrefix(): string { return $this->handle; } /** * @inheritdoc */ public function getCpEditUrl(): ?string { if (!$this->id || !Craft::$app->getUser()->getIsAdmin()) { return null; } return UrlHelper::cpUrl("settings/entry-types/$this->id"); } /** * Returns the entry type’s config. * * @return array * @since 3.5.0 */ public function getConfig(): array { $config = [ 'name' => $this->name, 'handle' => $this->handle, 'description' => $this->description ?: null, 'icon' => $this->icon || $this->icon === '0' ? $this->icon : null, 'color' => $this->color?->value, 'uiLabelFormat' => $this->uiLabelFormat, 'hasTitleField' => $this->hasTitleField, 'titleTranslationMethod' => $this->titleTranslationMethod, 'titleTranslationKeyFormat' => $this->titleTranslationKeyFormat ?: null, 'titleFormat' => $this->titleFormat ?: null, 'allowLineBreaksInTitles' => $this->allowLineBreaksInTitles, 'showSlugField' => $this->showSlugField, 'slugTranslationMethod' => $this->slugTranslationMethod, 'slugTranslationKeyFormat' => $this->slugTranslationKeyFormat ?: null, 'showStatusField' => $this->showStatusField, ]; $fieldLayout = $this->getFieldLayout(); if ($fieldLayoutConfig = $fieldLayout->getConfig()) { $config['fieldLayouts'] = [ $fieldLayout->uid => $fieldLayoutConfig, ]; } return $config; } /** * Returns the entry type’s usage info, possibly with name and handle override values. * * @return array * @since 5.6.0 */ public function getUsageConfig(): array { $config = ['uid' => $this->uid]; if (isset($this->original)) { if ($this->name !== $this->original->name) { $config['name'] = $this->name; } if ($this->handle !== $this->original->handle) { $config['handle'] = $this->handle; } if ($this->description !== $this->original->description) { $config['description'] = $this->description; } if (isset($this->group)) { $config['group'] = $this->group; } } return $config; } /** * Returns an array of sections and custom fields that make use of this entry type. * * @return array * @since 5.0.0 */ public function findUsages(): array { if (!isset($this->id)) { return []; } $usages = []; // Sections foreach (Craft::$app->getEntries()->getAllSections() as $section) { foreach ($section->getEntryTypes() as $entryType) { if ($entryType->id === $this->id) { $usages[] = $section; break; } } } // Fields $fieldsService = Craft::$app->getFields(); foreach ($fieldsService->getNestedEntryFieldTypes() as $type) { /** @var ElementContainerFieldInterface[] $fields */ $fields = $fieldsService->getFieldsByType($type); foreach ($fields as $field) { foreach ($field->getFieldLayoutProviders() as $provider) { if ($provider instanceof EntryType && $provider->id === $this->id) { $usages[] = $field; } } } } return $usages; } } An internal server error occurred.