| Total Complexity | 128 |
| Total Lines | 1105 |
| Duplicated Lines | 0 % |
| Changes | 17 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BaseElement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class BaseElement extends DataObject implements CMSPreviewable |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * Override this on your custom elements to specify a CSS icon class |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private static $icon = 'font-icon-block-layout'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Describe the purpose of this element |
||
| 62 | * |
||
| 63 | * @config |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private static $description = 'Base element class'; |
||
| 67 | |||
| 68 | private static $db = [ |
||
| 69 | 'Title' => 'Varchar(255)', |
||
| 70 | 'ShowTitle' => 'Boolean', |
||
| 71 | 'Sort' => 'Int', |
||
| 72 | 'ExtraClass' => 'Varchar(255)', |
||
| 73 | 'Style' => 'Varchar(255)' |
||
| 74 | ]; |
||
| 75 | |||
| 76 | private static $has_one = [ |
||
| 77 | 'Parent' => ElementalArea::class |
||
| 78 | ]; |
||
| 79 | |||
| 80 | private static $extensions = [ |
||
| 81 | Versioned::class |
||
| 82 | ]; |
||
| 83 | |||
| 84 | private static $casting = [ |
||
| 85 | 'BlockSchema' => DBObjectType::class, |
||
| 86 | 'IsLiveVersion' => DBBoolean::class, |
||
| 87 | 'IsPublished' => DBBoolean::class, |
||
| 88 | 'canCreate' => DBBoolean::class, |
||
| 89 | 'canPublish' => DBBoolean::class, |
||
| 90 | 'canUnpublish' => DBBoolean::class, |
||
| 91 | 'canDelete' => DBBoolean::class, |
||
| 92 | ]; |
||
| 93 | |||
| 94 | private static $indexes = [ |
||
| 95 | 'Sort' => true, |
||
| 96 | ]; |
||
| 97 | |||
| 98 | private static $versioned_gridfield_extensions = true; |
||
| 99 | |||
| 100 | private static $table_name = 'Element'; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | private static $controller_class = ElementController::class; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | private static $controller_template = 'ElementHolder'; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var ElementController |
||
| 114 | */ |
||
| 115 | protected $controller; |
||
| 116 | |||
| 117 | private static $show_stage_link = true; |
||
| 118 | |||
| 119 | private static $show_live_link = true; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Cache various data to improve CMS load time |
||
| 123 | * |
||
| 124 | * @internal |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected $cacheData; |
||
| 128 | |||
| 129 | private static $default_sort = 'Sort'; |
||
| 130 | |||
| 131 | private static $singular_name = 'block'; |
||
| 132 | |||
| 133 | private static $plural_name = 'blocks'; |
||
| 134 | |||
| 135 | private static $summary_fields = [ |
||
| 136 | 'EditorPreview' => 'Summary' |
||
| 137 | ]; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @config |
||
| 141 | * @var array |
||
| 142 | */ |
||
| 143 | private static $styles = []; |
||
| 144 | |||
| 145 | private static $searchable_fields = [ |
||
| 146 | 'ID' => [ |
||
| 147 | 'field' => NumericField::class, |
||
| 148 | ], |
||
| 149 | 'Title', |
||
| 150 | 'LastEdited' |
||
| 151 | ]; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Enable for backwards compatibility |
||
| 155 | * |
||
| 156 | * @var boolean |
||
| 157 | */ |
||
| 158 | private static $disable_pretty_anchor_name = false; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be |
||
| 162 | * clickable and a GridFieldDetailForm will be used. |
||
| 163 | * |
||
| 164 | * @config |
||
| 165 | * @var bool |
||
| 166 | */ |
||
| 167 | private static $inline_editable = true; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Display a show title button |
||
| 171 | * |
||
| 172 | * @config |
||
| 173 | * @var boolean |
||
| 174 | */ |
||
| 175 | private static $displays_title_in_template = true; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Determines whether a block should be indexable in search. |
||
| 179 | * |
||
| 180 | * @config |
||
| 181 | * @var boolean |
||
| 182 | * @see ElementalPageExtension::getElementsForSearch() |
||
| 183 | */ |
||
| 184 | private static $search_indexable = true; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Store used anchor names, this is to avoid title clashes |
||
| 188 | * when calling 'getAnchor' |
||
| 189 | * |
||
| 190 | * @var array |
||
| 191 | */ |
||
| 192 | protected static $used_anchors = []; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * For caching 'getAnchor' |
||
| 196 | * |
||
| 197 | * @var string |
||
| 198 | */ |
||
| 199 | protected $anchor = null; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Basic permissions, defaults to page perms where possible. |
||
| 203 | * |
||
| 204 | * @param Member $member |
||
| 205 | * @return boolean |
||
| 206 | */ |
||
| 207 | public function canView($member = null) |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Basic permissions, defaults to page perms where possible. |
||
| 225 | * |
||
| 226 | * @param Member $member |
||
| 227 | * |
||
| 228 | * @return boolean |
||
| 229 | */ |
||
| 230 | public function canEdit($member = null) |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Basic permissions, defaults to page perms where possible. |
||
| 248 | * |
||
| 249 | * Uses archive not delete so that current stage is respected i.e if a |
||
| 250 | * element is not published, then it can be deleted by someone who doesn't |
||
| 251 | * have publishing permissions. |
||
| 252 | * |
||
| 253 | * @param Member $member |
||
| 254 | * |
||
| 255 | * @return boolean |
||
| 256 | */ |
||
| 257 | public function canDelete($member = null) |
||
| 258 | { |
||
| 259 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 260 | |||
| 261 | if ($extended !== null) { |
||
| 262 | return $extended; |
||
| 263 | } |
||
| 264 | |||
| 265 | if ($this->hasMethod('getPage')) { |
||
| 266 | if ($page = $this->getPage()) { |
||
| 267 | if ($page->hasExtension(Versioned::class)) { |
||
| 268 | return $page->canArchive($member); |
||
| 269 | } else { |
||
| 270 | return $page->canDelete($member); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Basic permissions, defaults to page perms where possible. |
||
| 280 | * |
||
| 281 | * @param Member $member |
||
| 282 | * @param array $context |
||
| 283 | * |
||
| 284 | * @return boolean |
||
| 285 | */ |
||
| 286 | public function canCreate($member = null, $context = array()) |
||
| 287 | { |
||
| 288 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 289 | if ($extended !== null) { |
||
| 290 | return $extended; |
||
| 291 | } |
||
| 292 | |||
| 293 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Increment the sort order if one hasn't been already defined. This |
||
| 298 | * ensures that new elements are created at the end of the list by default. |
||
| 299 | * |
||
| 300 | * {@inheritDoc} |
||
| 301 | */ |
||
| 302 | public function onBeforeWrite() |
||
| 303 | { |
||
| 304 | parent::onBeforeWrite(); |
||
| 305 | |||
| 306 | // If a Sort has already been set, then we can exit early |
||
| 307 | if ($this->Sort) { |
||
| 308 | return; |
||
| 309 | } |
||
| 310 | |||
| 311 | // If no ParentID is currently set for the Element, then we don't want to define an initial Sort yet |
||
| 312 | if (!$this->ParentID) { |
||
| 313 | return; |
||
| 314 | } |
||
| 315 | |||
| 316 | if ($this->hasExtension(Versioned::class)) { |
||
| 317 | $records = Versioned::get_by_stage(BaseElement::class, Versioned::DRAFT); |
||
| 318 | } else { |
||
| 319 | $records = BaseElement::get(); |
||
| 320 | } |
||
| 321 | |||
| 322 | $records = $records->filter('ParentID', $this->ParentID); |
||
| 323 | |||
| 324 | $this->Sort = $records->max('Sort') + 1; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function getCMSFields() |
||
| 328 | { |
||
| 329 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
| 330 | // Remove relationship fields |
||
| 331 | $fields->removeByName('ParentID'); |
||
| 332 | $fields->removeByName('Sort'); |
||
| 333 | |||
| 334 | // Remove link and file tracking tabs |
||
| 335 | $fields->removeByName(['LinkTracking', 'FileTracking']); |
||
| 336 | |||
| 337 | $fields->addFieldToTab( |
||
| 338 | 'Root.Settings', |
||
| 339 | TextField::create('ExtraClass', _t(__CLASS__ . '.ExtraCssClassesLabel', 'Custom CSS classes')) |
||
| 340 | ->setAttribute( |
||
| 341 | 'placeholder', |
||
| 342 | _t(__CLASS__ . '.ExtraCssClassesPlaceholder', 'my_class another_class') |
||
| 343 | ) |
||
| 344 | ); |
||
| 345 | |||
| 346 | // Rename the "Settings" tab |
||
| 347 | $fields->fieldByName('Root.Settings') |
||
| 348 | ->setTitle(_t(__CLASS__ . '.SettingsTabLabel', 'Settings')); |
||
| 349 | |||
| 350 | // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group |
||
| 351 | $fields->removeByName('ShowTitle'); |
||
| 352 | |||
| 353 | if ($this->config()->get('displays_title_in_template')) { |
||
| 354 | $fields->replaceField( |
||
| 355 | 'Title', |
||
| 356 | TextCheckboxGroupField::create() |
||
| 357 | ->setName('Title') |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | |||
| 361 | // Rename the "Main" tab |
||
| 362 | $fields->fieldByName('Root.Main') |
||
| 363 | ->setTitle(_t(__CLASS__ . '.MainTabLabel', 'Content')); |
||
| 364 | |||
| 365 | $fields->addFieldsToTab('Root.Main', [ |
||
| 366 | HiddenField::create('AbsoluteLink', false, Director::absoluteURL($this->PreviewLink())), |
||
| 367 | HiddenField::create('LiveLink', false, Director::absoluteURL($this->Link())), |
||
| 368 | HiddenField::create('StageLink', false, Director::absoluteURL($this->PreviewLink())), |
||
| 369 | ]); |
||
| 370 | |||
| 371 | $styles = $this->config()->get('styles'); |
||
| 372 | |||
| 373 | if ($styles && count($styles ?? []) > 0) { |
||
| 374 | $styleDropdown = DropdownField::create('Style', _t(__CLASS__.'.STYLE', 'Style variation'), $styles); |
||
| 375 | |||
| 376 | $fields->insertBefore($styleDropdown, 'ExtraClass'); |
||
| 377 | |||
| 378 | $styleDropdown->setEmptyString(_t(__CLASS__.'.CUSTOM_STYLES', 'Select a style..')); |
||
| 379 | } else { |
||
| 380 | $fields->removeByName('Style'); |
||
| 381 | } |
||
| 382 | |||
| 383 | // Hide the navigation section of the tabs in the React component {@see silverstripe/admin Tabs} |
||
| 384 | $rootTabset = $fields->fieldByName('Root'); |
||
| 385 | $rootTabset->setSchemaState(['hideNav' => true]); |
||
| 386 | |||
| 387 | if ($this->isInDB()) { |
||
| 388 | $fields->addFieldsToTab('Root.History', [ |
||
| 389 | HistoryViewerField::create('ElementHistory') |
||
| 390 | ->addExtraClass('history-viewer--standalone'), |
||
| 391 | ]); |
||
| 392 | // Add class to containing tab |
||
| 393 | $fields->fieldByName('Root.History') |
||
| 394 | ->addExtraClass('elemental-block__history-tab tab--history-viewer'); |
||
| 395 | |||
| 396 | // Hack: automatically navigate to the History tab with `#Root_History` is in the URL |
||
| 397 | // To unhack, fix this: https://github.com/silverstripe/silverstripe-admin/issues/911 |
||
| 398 | Requirements::customScript(<<<JS |
||
| 399 | document.addEventListener('DOMContentLoaded', () => { |
||
| 400 | var hash = window.location.hash.substr(1); |
||
| 401 | if (hash !== 'Root_History') { |
||
| 402 | return null; |
||
| 403 | } |
||
| 404 | jQuery('.cms-tabset-nav-primary li[aria-controls="Root_History"] a').trigger('click') |
||
| 405 | }); |
||
| 406 | JS |
||
| 407 | ); |
||
| 408 | } |
||
| 409 | }); |
||
| 410 | |||
| 411 | return parent::getCMSFields(); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get the type of the current block, for use in GridField summaries, block |
||
| 416 | * type dropdowns etc. Examples are "Content", "File", "Media", etc. |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getType() |
||
| 421 | { |
||
| 422 | $default = $this->i18n_singular_name() ?: 'Block'; |
||
| 423 | |||
| 424 | return _t(__CLASS__ . '.BlockType', $default); |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Proxy through to configuration setting 'inline_editable' |
||
| 429 | * |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | public function inlineEditable() |
||
| 433 | { |
||
| 434 | return static::config()->get('inline_editable'); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param ElementController $controller |
||
| 439 | * |
||
| 440 | * @return $this |
||
| 441 | */ |
||
| 442 | public function setController($controller) |
||
| 443 | { |
||
| 444 | $this->controller = $controller; |
||
| 445 | |||
| 446 | return $this; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @throws Exception If the specified controller class doesn't exist |
||
| 451 | * |
||
| 452 | * @return ElementController |
||
| 453 | */ |
||
| 454 | public function getController() |
||
| 455 | { |
||
| 456 | if ($this->controller) { |
||
| 457 | return $this->controller; |
||
| 458 | } |
||
| 459 | |||
| 460 | $controllerClass = self::config()->controller_class; |
||
| 461 | |||
| 462 | if (!class_exists($controllerClass ?? '')) { |
||
| 463 | throw new Exception( |
||
| 464 | 'Could not find controller class ' . $controllerClass . ' as defined in ' . static::class |
||
| 465 | ); |
||
| 466 | } |
||
| 467 | |||
| 468 | $this->controller = Injector::inst()->create($controllerClass, $this); |
||
| 469 | $this->controller->doInit(); |
||
| 470 | |||
| 471 | return $this->controller; |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $name |
||
| 476 | * @return $this |
||
| 477 | */ |
||
| 478 | public function setAreaRelationNameCache($name) |
||
| 479 | { |
||
| 480 | $this->cacheData['area_relation_name'] = $name; |
||
| 481 | |||
| 482 | return $this; |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return Controller |
||
| 487 | */ |
||
| 488 | public function Top() |
||
| 489 | { |
||
| 490 | return (Controller::has_curr()) ? Controller::curr() : null; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Determines whether this elemental block is indexable in search. |
||
| 495 | * |
||
| 496 | * By default, this uses the configurable variable search_indexable, but |
||
| 497 | * this method can be overridden to provide more complex logic if required. |
||
| 498 | * |
||
| 499 | * @return boolean |
||
| 500 | */ |
||
| 501 | public function getSearchIndexable(): bool |
||
| 502 | { |
||
| 503 | return (bool) $this->config()->get('search_indexable'); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Provides content to be indexed in search. |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function getContentForSearchIndex(): string |
||
| 512 | { |
||
| 513 | // Strips tags but be sure there's a space between words. |
||
| 514 | $content = trim(strip_tags(str_replace('<', ' <', $this->forTemplate() ?? '') ?? '')); |
||
| 515 | // Allow projects to update indexable content of third-party elements. |
||
| 516 | $this->extend('updateContentForSearchIndex', $content); |
||
| 517 | return $content; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Default way to render element in templates. Note that all blocks should |
||
| 522 | * be rendered through their {@link ElementController} class as this |
||
| 523 | * contains the holder styles. |
||
| 524 | * |
||
| 525 | * @return string|null HTML |
||
| 526 | */ |
||
| 527 | public function forTemplate($holder = true) |
||
| 528 | { |
||
| 529 | $templates = $this->getRenderTemplates(); |
||
| 530 | |||
| 531 | if ($templates) { |
||
| 532 | return $this->renderWith($templates); |
||
| 533 | } |
||
| 534 | |||
| 535 | return null; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $suffix |
||
| 540 | * |
||
| 541 | * @return array |
||
| 542 | */ |
||
| 543 | public function getRenderTemplates($suffix = '') |
||
| 544 | { |
||
| 545 | $classes = ClassInfo::ancestry($this->ClassName); |
||
| 546 | $classes[static::class] = static::class; |
||
| 547 | $classes = array_reverse($classes ?? []); |
||
| 548 | $templates = []; |
||
| 549 | |||
| 550 | foreach ($classes as $key => $class) { |
||
| 551 | if ($class == BaseElement::class) { |
||
| 552 | continue; |
||
| 553 | } |
||
| 554 | |||
| 555 | if ($class == DataObject::class) { |
||
| 556 | break; |
||
| 557 | } |
||
| 558 | |||
| 559 | if ($style = $this->Style) { |
||
| 560 | $templates[$class][] = $class . $suffix . '_'. $this->getAreaRelationName() . '_' . $style; |
||
| 561 | $templates[$class][] = $class . $suffix . '_' . $style; |
||
| 562 | } |
||
| 563 | $templates[$class][] = $class . $suffix . '_'. $this->getAreaRelationName(); |
||
| 564 | $templates[$class][] = $class . $suffix; |
||
| 565 | } |
||
| 566 | |||
| 567 | $this->extend('updateRenderTemplates', $templates, $suffix); |
||
| 568 | |||
| 569 | $templateFlat = []; |
||
| 570 | foreach ($templates as $class => $variations) { |
||
| 571 | $templateFlat = array_merge($templateFlat, $variations); |
||
| 572 | } |
||
| 573 | |||
| 574 | return $templateFlat; |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Given form data (wit |
||
| 579 | * |
||
| 580 | * @param $data |
||
| 581 | */ |
||
| 582 | public function updateFromFormData($data) |
||
| 583 | { |
||
| 584 | $cmsFields = $this->getCMSFields(); |
||
| 585 | |||
| 586 | foreach ($data as $field => $datum) { |
||
| 587 | $field = $cmsFields->dataFieldByName($field); |
||
| 588 | |||
| 589 | if (!$field) { |
||
| 590 | continue; |
||
| 591 | } |
||
| 592 | |||
| 593 | $field->setSubmittedValue($datum); |
||
| 594 | $field->saveInto($this); |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Strip all namespaces from class namespace. |
||
| 600 | * |
||
| 601 | * @param string $classname e.g. "\Fully\Namespaced\Class" |
||
| 602 | * |
||
| 603 | * @return string following the param example, "Class" |
||
| 604 | */ |
||
| 605 | protected function stripNamespacing($classname) |
||
| 606 | { |
||
| 607 | $classParts = explode('\\', $classname ?? ''); |
||
| 608 | return array_pop($classParts); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | public function getSimpleClassName() |
||
| 615 | { |
||
| 616 | return strtolower($this->sanitiseClassName($this->ClassName, '__') ?? ''); |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Despite the name of the method, getPage can return any type of DataObject |
||
| 621 | * @return null|DataObject |
||
| 622 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 623 | * @throws \SilverStripe\ORM\ValidationException |
||
| 624 | */ |
||
| 625 | public function getPage() |
||
| 626 | { |
||
| 627 | // Allow for repeated calls to be cached |
||
| 628 | if (isset($this->cacheData['page'])) { |
||
| 629 | return $this->cacheData['page']; |
||
| 630 | } |
||
| 631 | |||
| 632 | $class = DataObject::getSchema()->hasOneComponent($this, 'Parent'); |
||
| 633 | $area = ($this->ParentID) ? DataObject::get_by_id($class, $this->ParentID) : null; |
||
| 634 | if ($area instanceof ElementalArea && $area->exists()) { |
||
| 635 | $this->cacheData['page'] = $area->getOwnerPage(); |
||
| 636 | return $this->cacheData['page']; |
||
| 637 | } |
||
| 638 | |||
| 639 | return null; |
||
| 640 | } |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Get a unique anchor name |
||
| 644 | * |
||
| 645 | * @return string |
||
| 646 | */ |
||
| 647 | public function getAnchor() |
||
| 648 | { |
||
| 649 | if ($this->anchor !== null) { |
||
| 650 | return $this->anchor; |
||
| 651 | } |
||
| 652 | |||
| 653 | $anchorTitle = ''; |
||
| 654 | |||
| 655 | if (!$this->config()->disable_pretty_anchor_name) { |
||
| 656 | if ($this->hasMethod('getAnchorTitle')) { |
||
| 657 | $anchorTitle = $this->getAnchorTitle(); |
||
| 658 | } elseif ($this->config()->enable_title_in_template) { |
||
| 659 | $anchorTitle = $this->getField('Title'); |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | if (!$anchorTitle) { |
||
| 664 | $anchorTitle = 'e'.$this->ID; |
||
| 665 | } |
||
| 666 | |||
| 667 | $filter = URLSegmentFilter::create(); |
||
| 668 | $titleAsURL = $filter->filter($anchorTitle); |
||
| 669 | |||
| 670 | // Ensure that this anchor name isn't already in use |
||
| 671 | // ie. If two elemental blocks have the same title, it'll append '-2', '-3' |
||
| 672 | $result = $titleAsURL; |
||
| 673 | $count = 1; |
||
| 674 | while (isset(self::$used_anchors[$result]) && self::$used_anchors[$result] !== $this->ID) { |
||
| 675 | ++$count; |
||
| 676 | $result = $titleAsURL . '-' . $count; |
||
| 677 | } |
||
| 678 | self::$used_anchors[$result] = $this->ID; |
||
| 679 | return $this->anchor = $result; |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @param string|null $action |
||
| 684 | * @return string|null |
||
| 685 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 686 | * @throws \SilverStripe\ORM\ValidationException |
||
| 687 | */ |
||
| 688 | public function AbsoluteLink($action = null) |
||
| 689 | { |
||
| 690 | $page = $this->getPage(); |
||
| 691 | |||
| 692 | if ($page && ClassInfo::hasMethod($page, 'AbsoluteLink')) { |
||
| 693 | $link = $page->AbsoluteLink($action) . '#' . $this->getAnchor(); |
||
| 694 | $this->extend('updateAbsoluteLink', $link); |
||
| 695 | |||
| 696 | return $link; |
||
| 697 | } |
||
| 698 | |||
| 699 | return null; |
||
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @param string|null $action |
||
| 704 | * @return string |
||
| 705 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 706 | * @throws \SilverStripe\ORM\ValidationException |
||
| 707 | */ |
||
| 708 | public function Link($action = null) |
||
| 709 | { |
||
| 710 | $page = $this->getPage(); |
||
| 711 | |||
| 712 | if ($page && ClassInfo::hasMethod($page, 'Link')) { |
||
| 713 | $link = $page->Link($action) . '#' . $this->getAnchor(); |
||
| 714 | $this->extend('updateLink', $link); |
||
| 715 | |||
| 716 | return $link; |
||
| 717 | } |
||
| 718 | |||
| 719 | return null; |
||
| 720 | } |
||
| 721 | |||
| 722 | /** |
||
| 723 | * @param string|null $action |
||
| 724 | * @return string |
||
| 725 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 726 | * @throws \SilverStripe\ORM\ValidationException |
||
| 727 | */ |
||
| 728 | public function PreviewLink($action = null) |
||
| 729 | { |
||
| 730 | $action = $action . '?ElementalPreview=' . mt_rand(); |
||
| 731 | $link = $this->Link($action); |
||
| 732 | $this->extend('updatePreviewLink', $link); |
||
| 733 | |||
| 734 | return $link; |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @return boolean |
||
| 739 | */ |
||
| 740 | public function isCMSPreview() |
||
| 741 | { |
||
| 742 | if (Controller::has_curr()) { |
||
| 743 | $controller = Controller::curr(); |
||
| 744 | |||
| 745 | if ($controller->getRequest()->requestVar('CMSPreview')) { |
||
| 746 | return true; |
||
| 747 | } |
||
| 748 | } |
||
| 749 | |||
| 750 | return false; |
||
| 751 | } |
||
| 752 | |||
| 753 | /** |
||
| 754 | * @param bool $directLink Indicates that the GridFieldDetailEdit form link should be given even if the block can be |
||
| 755 | * edited in-line. |
||
| 756 | * @return null|string |
||
| 757 | * @throws \SilverStripe\ORM\ValidationException |
||
| 758 | */ |
||
| 759 | public function CMSEditLink($directLink = false) |
||
| 760 | { |
||
| 761 | // Allow for repeated calls to be returned from cache |
||
| 762 | if (isset($this->cacheData['cms_edit_link'])) { |
||
| 763 | return $this->cacheData['cms_edit_link']; |
||
| 764 | } |
||
| 765 | |||
| 766 | $link = $this->getElementCMSLink($directLink); |
||
| 767 | $this->extend('updateCMSEditLink', $link); |
||
| 768 | |||
| 769 | if ($link) { |
||
| 770 | $this->cacheData['cms_edit_link'] = $link; |
||
| 771 | } |
||
| 772 | |||
| 773 | return $link; |
||
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 777 | * @param bool $directLink |
||
| 778 | * @return null|string |
||
| 779 | */ |
||
| 780 | private function getElementCMSLink(bool $directLink) |
||
| 781 | { |
||
| 782 | $relationName = $this->getAreaRelationName(); |
||
| 783 | $page = $this->getPage(); |
||
| 784 | |||
| 785 | $link = null; |
||
| 786 | |||
| 787 | if (!$page) { |
||
| 788 | return $link; |
||
| 789 | } |
||
| 790 | |||
| 791 | if ($page instanceof SiteTree) { |
||
| 792 | $link = $page->CMSEditLink(); |
||
| 793 | } elseif (ClassInfo::hasMethod($page, 'CMSEditLink')) { |
||
| 794 | $link = Controller::join_links($page->CMSEditLink(), 'ItemEditForm'); |
||
| 795 | } |
||
| 796 | // In-line editable blocks should just take you to the page. |
||
| 797 | // Editable ones should add the suffix for detail form. |
||
| 798 | if (!$this->inlineEditable() || $directLink) { |
||
| 799 | if ($page instanceof SiteTree) { |
||
| 800 | return Controller::join_links( |
||
| 801 | singleton(CMSPageEditController::class)->Link('EditForm'), |
||
| 802 | $page->ID, |
||
| 803 | 'field', |
||
| 804 | $relationName, |
||
| 805 | 'item', |
||
| 806 | $this->ID, |
||
| 807 | 'edit' |
||
| 808 | ); |
||
| 809 | } else { |
||
| 810 | // If $page is not a Page, then generate $link base on $page->CMSEditLink() |
||
| 811 | return Controller::join_links( |
||
| 812 | $link, |
||
| 813 | 'field', |
||
| 814 | $relationName, |
||
| 815 | 'item', |
||
| 816 | $this->ID, |
||
| 817 | 'edit' |
||
| 818 | ); |
||
| 819 | } |
||
| 820 | } |
||
| 821 | |||
| 822 | return $link; |
||
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Retrieve a elemental area relation for creating cms links |
||
| 827 | * |
||
| 828 | * @return int|string The name of a valid elemental area relation |
||
| 829 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 830 | * @throws \SilverStripe\ORM\ValidationException |
||
| 831 | */ |
||
| 832 | public function getAreaRelationName() |
||
| 833 | { |
||
| 834 | // Allow repeated calls to return from internal cache |
||
| 835 | if (isset($this->cacheData['area_relation_name'])) { |
||
| 836 | return $this->cacheData['area_relation_name']; |
||
| 837 | } |
||
| 838 | |||
| 839 | $page = $this->getPage(); |
||
| 840 | |||
| 841 | $result = 'ElementalArea'; |
||
| 842 | |||
| 843 | if ($page) { |
||
| 844 | $has_one = $page->config()->get('has_one'); |
||
| 845 | $area = $this->Parent(); |
||
| 846 | |||
| 847 | foreach ($has_one as $relationName => $relationClass) { |
||
| 848 | if ($page instanceof BaseElement && $relationName === 'Parent') { |
||
| 849 | continue; |
||
| 850 | } |
||
| 851 | if ($relationClass === $area->ClassName && $page->{$relationName}()->ID === $area->ID) { |
||
| 852 | $result = $relationName; |
||
| 853 | break; |
||
| 854 | } |
||
| 855 | } |
||
| 856 | } |
||
| 857 | |||
| 858 | $this->setAreaRelationNameCache($result); |
||
| 859 | |||
| 860 | return $result; |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Sanitise a model class' name for inclusion in a link. |
||
| 865 | * |
||
| 866 | * @return string |
||
| 867 | */ |
||
| 868 | public function sanitiseClassName($class, $delimiter = '-') |
||
| 869 | { |
||
| 870 | return str_replace('\\', $delimiter ?? '', $class ?? ''); |
||
| 871 | } |
||
| 872 | |||
| 873 | public function unsanitiseClassName($class, $delimiter = '-') |
||
| 874 | { |
||
| 875 | return str_replace($delimiter ?? '', '\\', $class ?? ''); |
||
| 876 | } |
||
| 877 | |||
| 878 | /** |
||
| 879 | * @return null|string |
||
| 880 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 881 | * @throws \SilverStripe\ORM\ValidationException |
||
| 882 | */ |
||
| 883 | public function getEditLink() |
||
| 884 | { |
||
| 885 | return Director::absoluteURL($this->CMSEditLink()); |
||
| 886 | } |
||
| 887 | |||
| 888 | /** |
||
| 889 | * @return DBField|null |
||
| 890 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 891 | * @throws \SilverStripe\ORM\ValidationException |
||
| 892 | */ |
||
| 893 | public function PageCMSEditLink() |
||
| 894 | { |
||
| 895 | if ($page = $this->getPage()) { |
||
| 896 | return DBField::create_field('HTMLText', sprintf( |
||
| 897 | '<a href="%s">%s</a>', |
||
| 898 | $page->CMSEditLink(), |
||
| 899 | $page->Title |
||
| 900 | )); |
||
| 901 | } |
||
| 902 | |||
| 903 | return null; |
||
| 904 | } |
||
| 905 | |||
| 906 | /** |
||
| 907 | * @return string |
||
| 908 | */ |
||
| 909 | public function getMimeType() |
||
| 910 | { |
||
| 911 | return 'text/html'; |
||
| 912 | } |
||
| 913 | |||
| 914 | /** |
||
| 915 | * This can be overridden on child elements to create a summary for display |
||
| 916 | * in GridFields. |
||
| 917 | * |
||
| 918 | * @return string |
||
| 919 | */ |
||
| 920 | public function getSummary() |
||
| 921 | { |
||
| 922 | return ''; |
||
| 923 | } |
||
| 924 | |||
| 925 | /** |
||
| 926 | * The block config defines a set of data (usually set through config on the element) that will be made available in |
||
| 927 | * client side config. Individual element types may choose to add config variable for use in React code |
||
| 928 | * |
||
| 929 | * @return array |
||
| 930 | */ |
||
| 931 | public static function getBlockConfig() |
||
| 932 | { |
||
| 933 | return []; |
||
| 934 | } |
||
| 935 | |||
| 936 | /** |
||
| 937 | * The block actions is an associative array available for providing data to the client side to be used to describe |
||
| 938 | * actions that may be performed. This is available as a plain "ObjectType" in the GraphQL schema. |
||
| 939 | * |
||
| 940 | * By default the only action is "edit" which is simply the URL where the block may be edited. |
||
| 941 | * |
||
| 942 | * To modify the actions, either use the extension point or overload the `provideBlockSchema` method. |
||
| 943 | * |
||
| 944 | * @internal This API may change in future. Treat this as a `final` method. |
||
| 945 | * @return array |
||
| 946 | */ |
||
| 947 | public function getBlockSchema() |
||
| 948 | { |
||
| 949 | $blockSchema = $this->provideBlockSchema(); |
||
| 950 | |||
| 951 | $this->extend('updateBlockSchema', $blockSchema); |
||
| 952 | |||
| 953 | return $blockSchema; |
||
| 954 | } |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Provide block schema data, which will be serialised and sent via GraphQL to the editor client. |
||
| 958 | * |
||
| 959 | * Overload this method in child element classes to augment, or use the extension point on `getBlockSchema` |
||
| 960 | * to update it from an `Extension`. |
||
| 961 | * |
||
| 962 | * @return array |
||
| 963 | * @throws SchemaBuilderException |
||
| 964 | * @throws ValidationException |
||
| 965 | */ |
||
| 966 | protected function provideBlockSchema() |
||
| 972 | ], |
||
| 973 | ]; |
||
| 974 | } |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Generate markup for element type icons suitable for use in GridFields. |
||
| 978 | * |
||
| 979 | * @return null|DBHTMLText |
||
| 980 | */ |
||
| 981 | public function getIcon() |
||
| 982 | { |
||
| 983 | $data = ArrayData::create([]); |
||
| 984 | |||
| 985 | $iconClass = $this->config()->get('icon'); |
||
| 986 | if ($iconClass) { |
||
| 987 | $data->IconClass = $iconClass; |
||
| 988 | |||
| 989 | // Add versioned states (rendered as a circle over the icon) |
||
| 990 | if ($this->hasExtension(Versioned::class)) { |
||
| 991 | $data->IsVersioned = true; |
||
| 992 | if ($this->isOnDraftOnly()) { |
||
| 993 | $data->VersionState = 'draft'; |
||
| 994 | $data->VersionStateTitle = _t( |
||
| 995 | 'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.ADDEDTODRAFTHELP', |
||
| 996 | 'Item has not been published yet' |
||
| 997 | ); |
||
| 998 | } elseif ($this->isModifiedOnDraft()) { |
||
| 999 | $data->VersionState = 'modified'; |
||
| 1000 | $data->VersionStateTitle = $data->VersionStateTitle = _t( |
||
| 1001 | 'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.MODIFIEDONDRAFTHELP', |
||
| 1002 | 'Item has unpublished changes' |
||
| 1003 | ); |
||
| 1004 | } |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | return $data->renderWith(__CLASS__ . '/PreviewIcon'); |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | return null; |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Get a description for this content element, if available |
||
| 1015 | * |
||
| 1016 | * @return string |
||
| 1017 | */ |
||
| 1018 | public function getDescription() |
||
| 1019 | { |
||
| 1020 | $description = $this->config()->uninherited('description'); |
||
| 1021 | if ($description) { |
||
| 1022 | return _t(__CLASS__ . '.Description', $description); |
||
| 1023 | } |
||
| 1024 | return ''; |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Generate markup for element type, with description suitable for use in |
||
| 1029 | * GridFields. |
||
| 1030 | * |
||
| 1031 | * @return DBField |
||
| 1032 | */ |
||
| 1033 | public function getTypeNice() |
||
| 1034 | { |
||
| 1035 | $description = $this->getDescription(); |
||
| 1036 | $desc = ($description) ? ' <span class="element__note"> — ' . $description . '</span>' : ''; |
||
| 1037 | |||
| 1038 | return DBField::create_field( |
||
| 1039 | 'HTMLVarchar', |
||
| 1040 | $this->getType() . $desc |
||
| 1041 | ); |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * @return \SilverStripe\ORM\FieldType\DBHTMLText |
||
| 1046 | */ |
||
| 1047 | public function getEditorPreview() |
||
| 1048 | { |
||
| 1049 | $templates = $this->getRenderTemplates('_EditorPreview'); |
||
| 1050 | $templates[] = BaseElement::class . '_EditorPreview'; |
||
| 1051 | |||
| 1052 | return $this->renderWith($templates); |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * @return Member |
||
| 1057 | */ |
||
| 1058 | public function getAuthor() |
||
| 1059 | { |
||
| 1060 | if ($this->AuthorID) { |
||
| 1061 | return Member::get()->byId($this->AuthorID); |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | return null; |
||
| 1065 | } |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Get a user defined style variant for this element, if available |
||
| 1069 | * |
||
| 1070 | * @return string |
||
| 1071 | */ |
||
| 1072 | public function getStyleVariant() |
||
| 1073 | { |
||
| 1074 | $style = $this->Style; |
||
| 1075 | $styles = $this->config()->get('styles'); |
||
| 1076 | |||
| 1077 | if (isset($styles[$style])) { |
||
| 1078 | $style = strtolower($style ?? ''); |
||
| 1079 | } else { |
||
| 1080 | $style = ''; |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | $this->extend('updateStyleVariant', $style); |
||
| 1084 | |||
| 1085 | return $style; |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * @return mixed|null |
||
| 1090 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 1091 | * @throws \SilverStripe\ORM\ValidationException |
||
| 1092 | */ |
||
| 1093 | public function getPageTitle() |
||
| 1094 | { |
||
| 1095 | $page = $this->getPage(); |
||
| 1096 | |||
| 1097 | if ($page) { |
||
| 1098 | return $page->Title; |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | return null; |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * @return boolean |
||
| 1106 | */ |
||
| 1107 | public function First() |
||
| 1108 | { |
||
| 1109 | return ($this->Parent()->Elements()->first()->ID === $this->ID); |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * @return boolean |
||
| 1114 | */ |
||
| 1115 | public function Last() |
||
| 1116 | { |
||
| 1117 | return ($this->Parent()->Elements()->last()->ID === $this->ID); |
||
| 1118 | } |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * @return int |
||
| 1122 | */ |
||
| 1123 | public function TotalItems() |
||
| 1124 | { |
||
| 1125 | return $this->Parent()->Elements()->count(); |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * Returns the position of the current element. |
||
| 1130 | * |
||
| 1131 | * @return int |
||
| 1132 | */ |
||
| 1133 | public function Pos() |
||
| 1136 | } |
||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * @return string |
||
| 1140 | */ |
||
| 1141 | public function EvenOdd() |
||
| 1142 | { |
||
| 1143 | $odd = (bool) ($this->Pos() % 2); |
||
| 1144 | |||
| 1145 | return ($odd) ? 'odd' : 'even'; |
||
| 1146 | } |
||
| 1147 | |||
| 1148 | /** |
||
| 1149 | * @return string |
||
| 1150 | */ |
||
| 1151 | public static function getGraphQLTypeName(): string |
||
| 1152 | { |
||
| 1153 | return class_exists(StaticSchema::class) |
||
| 1154 | ? StaticSchema::inst()->typeNameForDataObject(static::class) |
||
| 1155 | : str_replace('\\', '_', static::class); |
||
| 1156 | } |
||
| 1157 | } |
||
| 1158 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths