| Total Complexity | 97 |
| Total Lines | 817 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 44 | class BaseElement extends DataObject |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * Override this on your custom elements to specify a CSS icon class |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private static $icon = 'font-icon-block-layout'; |
||
|
|
|||
| 52 | |||
| 53 | /** |
||
| 54 | * Describe the purpose of this element |
||
| 55 | * |
||
| 56 | * @config |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private static $description = 'Base element class'; |
||
| 60 | |||
| 61 | private static $db = [ |
||
| 62 | 'Title' => 'Varchar(255)', |
||
| 63 | 'ShowTitle' => 'Boolean', |
||
| 64 | 'Sort' => 'Int', |
||
| 65 | 'ExtraClass' => 'Varchar(255)', |
||
| 66 | 'Style' => 'Varchar(255)' |
||
| 67 | ]; |
||
| 68 | |||
| 69 | private static $has_one = [ |
||
| 70 | 'Parent' => ElementalArea::class |
||
| 71 | ]; |
||
| 72 | |||
| 73 | private static $extensions = [ |
||
| 74 | Versioned::class |
||
| 75 | ]; |
||
| 76 | |||
| 77 | private static $casting = [ |
||
| 78 | 'SummaryData' => DBObjectType::class, |
||
| 79 | ]; |
||
| 80 | |||
| 81 | private static $versioned_gridfield_extensions = true; |
||
| 82 | |||
| 83 | private static $table_name = 'Element'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | private static $controller_class = ElementController::class; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | private static $controller_template = 'ElementHolder'; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var ElementController |
||
| 97 | */ |
||
| 98 | protected $controller; |
||
| 99 | |||
| 100 | private static $default_sort = 'Sort'; |
||
| 101 | |||
| 102 | private static $singular_name = 'block'; |
||
| 103 | |||
| 104 | private static $plural_name = 'blocks'; |
||
| 105 | |||
| 106 | private static $summary_fields = [ |
||
| 107 | 'EditorPreview' => 'Summary' |
||
| 108 | ]; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @config |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | private static $styles = []; |
||
| 115 | |||
| 116 | private static $searchable_fields = [ |
||
| 117 | 'ID' => [ |
||
| 118 | 'field' => NumericField::class, |
||
| 119 | ], |
||
| 120 | 'Title', |
||
| 121 | 'LastEdited' |
||
| 122 | ]; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Enable for backwards compatibility |
||
| 126 | * |
||
| 127 | * @var boolean |
||
| 128 | */ |
||
| 129 | private static $disable_pretty_anchor_name = false; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Store used anchor names, this is to avoid title clashes |
||
| 133 | * when calling 'getAnchor' |
||
| 134 | * |
||
| 135 | * @var array |
||
| 136 | */ |
||
| 137 | protected static $used_anchors = []; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * For caching 'getAnchor' |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $anchor = null; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Basic permissions, defaults to page perms where possible. |
||
| 148 | * |
||
| 149 | * @param Member $member |
||
| 150 | * @return boolean |
||
| 151 | */ |
||
| 152 | public function canView($member = null) |
||
| 153 | { |
||
| 154 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 155 | if ($extended !== null) { |
||
| 156 | return $extended; |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($this->hasMethod('getPage')) { |
||
| 160 | if ($page = $this->getPage()) { |
||
| 161 | return $page->canView($member); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Basic permissions, defaults to page perms where possible. |
||
| 170 | * |
||
| 171 | * @param Member $member |
||
| 172 | * |
||
| 173 | * @return boolean |
||
| 174 | */ |
||
| 175 | public function canEdit($member = null) |
||
| 176 | { |
||
| 177 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 178 | if ($extended !== null) { |
||
| 179 | return $extended; |
||
| 180 | } |
||
| 181 | |||
| 182 | if ($this->hasMethod('getPage')) { |
||
| 183 | if ($page = $this->getPage()) { |
||
| 184 | return $page->canEdit($member); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Basic permissions, defaults to page perms where possible. |
||
| 193 | * |
||
| 194 | * Uses archive not delete so that current stage is respected i.e if a |
||
| 195 | * element is not published, then it can be deleted by someone who doesn't |
||
| 196 | * have publishing permissions. |
||
| 197 | * |
||
| 198 | * @param Member $member |
||
| 199 | * |
||
| 200 | * @return boolean |
||
| 201 | */ |
||
| 202 | public function canDelete($member = null) |
||
| 203 | { |
||
| 204 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 205 | if ($extended !== null) { |
||
| 206 | return $extended; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($this->hasMethod('getPage')) { |
||
| 210 | if ($page = $this->getPage()) { |
||
| 211 | return $page->canArchive($member); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Basic permissions, defaults to page perms where possible. |
||
| 220 | * |
||
| 221 | * @param Member $member |
||
| 222 | * @param array $context |
||
| 223 | * |
||
| 224 | * @return boolean |
||
| 225 | */ |
||
| 226 | public function canCreate($member = null, $context = array()) |
||
| 227 | { |
||
| 228 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 229 | if ($extended !== null) { |
||
| 230 | return $extended; |
||
| 231 | } |
||
| 232 | |||
| 233 | return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Increment the sort order if one hasn't been already defined. This ensures that new elements are created |
||
| 238 | * at the end of the list by default. |
||
| 239 | * |
||
| 240 | * {@inheritDoc} |
||
| 241 | */ |
||
| 242 | public function onBeforeWrite() |
||
| 243 | { |
||
| 244 | parent::onBeforeWrite(); |
||
| 245 | |||
| 246 | if (!$this->Sort) { |
||
| 247 | $this->Sort = static::get()->max('Sort') + 1; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | public function getCMSFields() |
||
| 252 | { |
||
| 253 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
| 254 | // Remove relationship fields |
||
| 255 | $fields->removeByName('ParentID'); |
||
| 256 | $fields->removeByName('Sort'); |
||
| 257 | |||
| 258 | $fields->addFieldToTab( |
||
| 259 | 'Root.Settings', |
||
| 260 | TextField::create('ExtraClass', _t(__CLASS__ . '.ExtraCssClassesLabel', 'Custom CSS classes')) |
||
| 261 | ->setAttribute( |
||
| 262 | 'placeholder', |
||
| 263 | _t(__CLASS__ . '.ExtraCssClassesPlaceholder', 'my_class another_class') |
||
| 264 | ) |
||
| 265 | ); |
||
| 266 | |||
| 267 | // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group |
||
| 268 | $fields->removeByName('ShowTitle'); |
||
| 269 | $fields->replaceField( |
||
| 270 | 'Title', |
||
| 271 | TextCheckboxGroupField::create( |
||
| 272 | TextField::create('Title', _t(__CLASS__ . '.TitleLabel', 'Title (displayed if checked)')), |
||
| 273 | CheckboxField::create('ShowTitle', _t(__CLASS__ . '.ShowTitleLabel', 'Displayed')) |
||
| 274 | ) |
||
| 275 | ->setName('TitleAndDisplayed') |
||
| 276 | ); |
||
| 277 | |||
| 278 | // Rename the "Main" tab |
||
| 279 | $fields->fieldByName('Root.Main') |
||
| 280 | ->setTitle(_t(__CLASS__ . '.MainTabLabel', 'Content')); |
||
| 281 | |||
| 282 | $fields->addFieldsToTab('Root.Main', [ |
||
| 283 | HiddenField::create('AbsoluteLink', false, Director::absoluteURL($this->PreviewLink())), |
||
| 284 | HiddenField::create('LiveLink', false, Director::absoluteURL($this->Link())), |
||
| 285 | HiddenField::create('StageLink', false, Director::absoluteURL($this->PreviewLink())), |
||
| 286 | ]); |
||
| 287 | |||
| 288 | $styles = $this->config()->get('styles'); |
||
| 289 | |||
| 290 | if ($styles && count($styles) > 0) { |
||
| 291 | $styleDropdown = DropdownField::create('Style', _t(__CLASS__.'.STYLE', 'Style variation'), $styles); |
||
| 292 | |||
| 293 | $fields->insertBefore($styleDropdown, 'ExtraClass'); |
||
| 294 | |||
| 295 | $styleDropdown->setEmptyString(_t(__CLASS__.'.CUSTOM_STYLES', 'Select a style..')); |
||
| 296 | } else { |
||
| 297 | $fields->removeByName('Style'); |
||
| 298 | } |
||
| 299 | |||
| 300 | // Support for new history viewer in SS 4.2+ |
||
| 301 | if (class_exists(HistoryViewerField::class)) { |
||
| 302 | Requirements::javascript('dnadesign/silverstripe-elemental:client/dist/js/bundle.js'); |
||
| 303 | |||
| 304 | $historyViewer = HistoryViewerField::create('ElementHistory'); |
||
| 305 | $fields->addFieldToTab('Root.History', $historyViewer); |
||
| 306 | |||
| 307 | $fields->fieldByName('Root.History') |
||
| 308 | ->addExtraClass('elemental-block__history-tab tab--history-viewer'); |
||
| 309 | } |
||
| 310 | }); |
||
| 311 | |||
| 312 | return parent::getCMSFields(); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get the type of the current block, for use in GridField summaries, block |
||
| 317 | * type dropdowns etc. Examples are "Content", "File", "Media", etc. |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function getType() |
||
| 322 | { |
||
| 323 | return _t(__CLASS__ . '.BlockType', 'Block'); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param ElementController $controller |
||
| 328 | * |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function setController($controller) |
||
| 332 | { |
||
| 333 | $this->controller = $controller; |
||
| 334 | |||
| 335 | return $this; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @throws Exception If the specified controller class doesn't exist |
||
| 340 | * |
||
| 341 | * @return ElementController |
||
| 342 | */ |
||
| 343 | public function getController() |
||
| 344 | { |
||
| 345 | if ($this->controller) { |
||
| 346 | return $this->controller; |
||
| 347 | } |
||
| 348 | |||
| 349 | $controllerClass = self::config()->controller_class; |
||
| 350 | |||
| 351 | if (!class_exists($controllerClass)) { |
||
| 352 | throw new Exception( |
||
| 353 | 'Could not find controller class ' . $controllerClass . ' as defined in ' . static::class |
||
| 354 | ); |
||
| 355 | } |
||
| 356 | |||
| 357 | $this->controller = Injector::inst()->create($controllerClass, $this); |
||
| 358 | $this->controller->doInit(); |
||
| 359 | |||
| 360 | return $this->controller; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return Controller |
||
| 365 | */ |
||
| 366 | public function Top() |
||
| 367 | { |
||
| 368 | return (Controller::has_curr()) ? Controller::curr() : null; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Default way to render element in templates. Note that all blocks should |
||
| 373 | * be rendered through their {@link ElementController} class as this |
||
| 374 | * contains the holder styles. |
||
| 375 | * |
||
| 376 | * @return string|null HTML |
||
| 377 | */ |
||
| 378 | public function forTemplate($holder = true) |
||
| 379 | { |
||
| 380 | $templates = $this->getRenderTemplates(); |
||
| 381 | |||
| 382 | if ($templates) { |
||
| 383 | return $this->renderWith($templates); |
||
| 384 | } |
||
| 385 | |||
| 386 | return null; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $suffix |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function getRenderTemplates($suffix = '') |
||
| 395 | { |
||
| 396 | $classes = ClassInfo::ancestry($this->ClassName); |
||
| 397 | $classes[static::class] = static::class; |
||
| 398 | $classes = array_reverse($classes); |
||
| 399 | $templates = []; |
||
| 400 | |||
| 401 | foreach ($classes as $key => $class) { |
||
| 402 | if ($class == BaseElement::class) { |
||
| 403 | continue; |
||
| 404 | } |
||
| 405 | |||
| 406 | if ($class == DataObject::class) { |
||
| 407 | break; |
||
| 408 | } |
||
| 409 | |||
| 410 | if ($style = $this->Style) { |
||
| 411 | $templates[$class][] = $class . $suffix . '_'. $this->getAreaRelationName() . '_' . $style; |
||
| 412 | $templates[$class][] = $class . $suffix . '_' . $style; |
||
| 413 | } |
||
| 414 | $templates[$class][] = $class . $suffix . '_'. $this->getAreaRelationName(); |
||
| 415 | $templates[$class][] = $class . $suffix; |
||
| 416 | } |
||
| 417 | |||
| 418 | $this->extend('updateRenderTemplates', $templates, $suffix); |
||
| 419 | |||
| 420 | $templateFlat = []; |
||
| 421 | foreach ($templates as $class => $variations) { |
||
| 422 | $templateFlat = array_merge($templateFlat, $variations); |
||
| 423 | } |
||
| 424 | |||
| 425 | return $templateFlat; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Strip all namespaces from class namespace. |
||
| 430 | * |
||
| 431 | * @param string $classname e.g. "\Fully\Namespaced\Class" |
||
| 432 | * |
||
| 433 | * @return string following the param example, "Class" |
||
| 434 | */ |
||
| 435 | protected function stripNamespacing($classname) |
||
| 436 | { |
||
| 437 | $classParts = explode('\\', $classname); |
||
| 438 | return array_pop($classParts); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public function getSimpleClassName() |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @return null|DataObject |
||
| 451 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 452 | * @throws \SilverStripe\ORM\ValidationException |
||
| 453 | */ |
||
| 454 | public function getPage() |
||
| 455 | { |
||
| 456 | $area = $this->Parent(); |
||
| 457 | |||
| 458 | if ($area instanceof ElementalArea && $area->exists()) { |
||
| 459 | return $area->getOwnerPage(); |
||
| 460 | } |
||
| 461 | |||
| 462 | return null; |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Get a unique anchor name |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public function getAnchor() |
||
| 471 | { |
||
| 472 | if ($this->anchor !== null) { |
||
| 473 | return $this->anchor; |
||
| 474 | } |
||
| 475 | |||
| 476 | $anchorTitle = ''; |
||
| 477 | |||
| 478 | if (!$this->config()->disable_pretty_anchor_name) { |
||
| 479 | if ($this->hasMethod('getAnchorTitle')) { |
||
| 480 | $anchorTitle = $this->getAnchorTitle(); |
||
| 481 | } elseif ($this->config()->enable_title_in_template) { |
||
| 482 | $anchorTitle = $this->getField('Title'); |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | if (!$anchorTitle) { |
||
| 487 | $anchorTitle = 'e'.$this->ID; |
||
| 488 | } |
||
| 489 | |||
| 490 | $filter = URLSegmentFilter::create(); |
||
| 491 | $titleAsURL = $filter->filter($anchorTitle); |
||
| 492 | |||
| 493 | // Ensure that this anchor name isn't already in use |
||
| 494 | // ie. If two elemental blocks have the same title, it'll append '-2', '-3' |
||
| 495 | $result = $titleAsURL; |
||
| 496 | $count = 1; |
||
| 497 | while (isset(self::$used_anchors[$result]) && self::$used_anchors[$result] !== $this->ID) { |
||
| 498 | ++$count; |
||
| 499 | $result = $titleAsURL . '-' . $count; |
||
| 500 | } |
||
| 501 | self::$used_anchors[$result] = $this->ID; |
||
| 502 | return $this->anchor = $result; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param string|null $action |
||
| 507 | * @return string|null |
||
| 508 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 509 | * @throws \SilverStripe\ORM\ValidationException |
||
| 510 | */ |
||
| 511 | public function AbsoluteLink($action = null) |
||
| 512 | { |
||
| 513 | if ($page = $this->getPage()) { |
||
| 514 | $link = $page->AbsoluteLink($action) . '#' . $this->getAnchor(); |
||
| 515 | |||
| 516 | return $link; |
||
| 517 | } |
||
| 518 | |||
| 519 | return null; |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param string|null $action |
||
| 524 | * @return string |
||
| 525 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 526 | * @throws \SilverStripe\ORM\ValidationException |
||
| 527 | */ |
||
| 528 | public function Link($action = null) |
||
| 529 | { |
||
| 530 | if ($page = $this->getPage()) { |
||
| 531 | $link = $page->Link($action) . '#' . $this->getAnchor(); |
||
| 532 | |||
| 533 | $this->extend('updateLink', $link); |
||
| 534 | |||
| 535 | return $link; |
||
| 536 | } |
||
| 537 | |||
| 538 | return null; |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param string|null $action |
||
| 543 | * @return string |
||
| 544 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 545 | * @throws \SilverStripe\ORM\ValidationException |
||
| 546 | */ |
||
| 547 | public function PreviewLink($action = null) |
||
| 548 | { |
||
| 549 | $action = $action . '?ElementalPreview=' . mt_rand(); |
||
| 550 | $link = $this->Link($action); |
||
| 551 | $this->extend('updatePreviewLink', $link); |
||
| 552 | |||
| 553 | return $link; |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @return boolean |
||
| 558 | */ |
||
| 559 | public function isCMSPreview() |
||
| 560 | { |
||
| 561 | if (Controller::has_curr()) { |
||
| 562 | $controller = Controller::curr(); |
||
| 563 | |||
| 564 | if ($controller->getRequest()->requestVar('CMSPreview')) { |
||
| 565 | return true; |
||
| 566 | } |
||
| 567 | } |
||
| 568 | |||
| 569 | return false; |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @return null|string |
||
| 574 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 575 | * @throws \SilverStripe\ORM\ValidationException |
||
| 576 | */ |
||
| 577 | public function CMSEditLink() |
||
| 578 | { |
||
| 579 | $relationName = $this->getAreaRelationName(); |
||
| 580 | $page = $this->getPage(true); |
||
| 581 | |||
| 582 | if (!$page) { |
||
| 583 | return null; |
||
| 584 | } |
||
| 585 | |||
| 586 | $editLinkPrefix = ''; |
||
| 587 | if (!$page instanceof SiteTree && method_exists($page, 'CMSEditLink')) { |
||
| 588 | $editLinkPrefix = Controller::join_links($page->CMSEditLink(), 'ItemEditForm'); |
||
| 589 | } else { |
||
| 590 | $editLinkPrefix = Controller::join_links( |
||
| 591 | singleton(CMSPageEditController::class)->Link('EditForm'), |
||
| 592 | $page->ID |
||
| 593 | ); |
||
| 594 | } |
||
| 595 | |||
| 596 | $link = Controller::join_links( |
||
| 597 | $editLinkPrefix, |
||
| 598 | 'field/' . $relationName . '/item/', |
||
| 599 | $this->ID |
||
| 600 | ); |
||
| 601 | |||
| 602 | $link = Controller::join_links( |
||
| 603 | $link, |
||
| 604 | 'edit' |
||
| 605 | ); |
||
| 606 | |||
| 607 | $this->extend('updateCMSEditLink', $link); |
||
| 608 | |||
| 609 | return $link; |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Retrieve a elemental area relation for creating cms links |
||
| 614 | * |
||
| 615 | * @return int|string The name of a valid elemental area relation |
||
| 616 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 617 | * @throws \SilverStripe\ORM\ValidationException |
||
| 618 | */ |
||
| 619 | public function getAreaRelationName() |
||
| 620 | { |
||
| 621 | $page = $this->getPage(); |
||
| 622 | |||
| 623 | if ($page) { |
||
| 624 | $has_one = $page->config()->get('has_one'); |
||
| 625 | $area = $this->Parent(); |
||
| 626 | |||
| 627 | foreach ($has_one as $relationName => $relationClass) { |
||
| 628 | if ($page instanceof BaseElement && $relationName === 'Parent') { |
||
| 629 | continue; |
||
| 630 | } |
||
| 631 | if ($relationClass === $area->ClassName && $page->{$relationName}()->ID === $area->ID) { |
||
| 632 | return $relationName; |
||
| 633 | } |
||
| 634 | } |
||
| 635 | } |
||
| 636 | |||
| 637 | return 'ElementalArea'; |
||
| 638 | } |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Sanitise a model class' name for inclusion in a link. |
||
| 642 | * |
||
| 643 | * @return string |
||
| 644 | */ |
||
| 645 | public function sanitiseClassName($class, $delimiter = '-') |
||
| 648 | } |
||
| 649 | |||
| 650 | public function unsanitiseClassName($class, $delimiter = '-') |
||
| 651 | { |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @return null|string |
||
| 657 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 658 | * @throws \SilverStripe\ORM\ValidationException |
||
| 659 | */ |
||
| 660 | public function getEditLink() |
||
| 661 | { |
||
| 662 | return $this->CMSEditLink(); |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @return DBField|null |
||
| 667 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 668 | * @throws \SilverStripe\ORM\ValidationException |
||
| 669 | */ |
||
| 670 | public function PageCMSEditLink() |
||
| 671 | { |
||
| 672 | if ($page = $this->getPage()) { |
||
| 673 | return DBField::create_field('HTMLText', sprintf( |
||
| 674 | '<a href="%s">%s</a>', |
||
| 675 | $page->CMSEditLink(), |
||
| 676 | $page->Title |
||
| 677 | )); |
||
| 678 | } |
||
| 679 | |||
| 680 | return null; |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @return string |
||
| 685 | */ |
||
| 686 | public function getMimeType() |
||
| 687 | { |
||
| 688 | return 'text/html'; |
||
| 689 | } |
||
| 690 | |||
| 691 | /** |
||
| 692 | * This can be overridden on child elements to create a summary for display |
||
| 693 | * in GridFields. |
||
| 694 | * |
||
| 695 | * @return string |
||
| 696 | */ |
||
| 697 | public function getSummary() |
||
| 700 | } |
||
| 701 | |||
| 702 | |||
| 703 | /** |
||
| 704 | * @return array |
||
| 705 | */ |
||
| 706 | public function getSummaryData() |
||
| 707 | { |
||
| 708 | $summaryData = []; |
||
| 709 | |||
| 710 | $this->extend('updateSummaryData', $summaryData); |
||
| 711 | |||
| 712 | return $summaryData; |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * This can be overridden on child elements. |
||
| 717 | * |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | public function getContent() |
||
| 723 | } |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Return the icon class name |
||
| 727 | * |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | public function getIconClass() |
||
| 731 | { |
||
| 732 | return $this->config()->get('icon'); |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Generate markup for element type icons suitable for use in GridFields. |
||
| 737 | * |
||
| 738 | * @return null|DBHTMLText |
||
| 739 | */ |
||
| 740 | public function getIcon() |
||
| 741 | { |
||
| 742 | $data = ArrayData::create([]); |
||
| 743 | |||
| 744 | $iconClass = $this->config()->get('icon'); |
||
| 745 | if ($iconClass) { |
||
| 746 | $data->IconClass = $iconClass; |
||
| 747 | |||
| 748 | // Add versioned states (rendered as a circle over the icon) |
||
| 749 | if ($this->hasExtension(Versioned::class)) { |
||
| 750 | $data->IsVersioned = true; |
||
| 751 | if ($this->isOnDraftOnly()) { |
||
| 752 | $data->VersionState = 'draft'; |
||
| 753 | $data->VersionStateTitle = _t( |
||
| 754 | 'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.ADDEDTODRAFTHELP', |
||
| 755 | 'Item has not been published yet' |
||
| 756 | ); |
||
| 757 | } elseif ($this->isModifiedOnDraft()) { |
||
| 758 | $data->VersionState = 'modified'; |
||
| 759 | $data->VersionStateTitle = $data->VersionStateTitle = _t( |
||
| 760 | 'SilverStripe\\Versioned\\VersionedGridFieldState\\VersionedGridFieldState.MODIFIEDONDRAFTHELP', |
||
| 761 | 'Item has unpublished changes' |
||
| 762 | ); |
||
| 763 | } |
||
| 764 | } |
||
| 765 | |||
| 766 | return $data->renderWith(__CLASS__ . '/PreviewIcon'); |
||
| 767 | } |
||
| 768 | |||
| 769 | return null; |
||
| 770 | } |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Get a description for this content element, if available |
||
| 774 | * |
||
| 775 | * @return string |
||
| 776 | */ |
||
| 777 | public function getDescription() |
||
| 778 | { |
||
| 779 | $description = $this->config()->uninherited('description'); |
||
| 780 | if ($description) { |
||
| 781 | return _t(__CLASS__ . '.Description', $description); |
||
| 782 | } |
||
| 783 | return ''; |
||
| 784 | } |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Generate markup for element type, with description suitable for use in |
||
| 788 | * GridFields. |
||
| 789 | * |
||
| 790 | * @return DBField |
||
| 791 | */ |
||
| 792 | public function getTypeNice() |
||
| 793 | { |
||
| 794 | $description = $this->getDescription(); |
||
| 795 | $desc = ($description) ? ' <span class="element__note"> — ' . $description . '</span>' : ''; |
||
| 796 | |||
| 797 | return DBField::create_field( |
||
| 798 | 'HTMLVarchar', |
||
| 799 | $this->getType() . $desc |
||
| 800 | ); |
||
| 801 | } |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @return \SilverStripe\ORM\FieldType\DBHTMLText |
||
| 805 | */ |
||
| 806 | public function getEditorPreview() |
||
| 807 | { |
||
| 808 | $templates = $this->getRenderTemplates('_EditorPreview'); |
||
| 809 | $templates[] = BaseElement::class . '_EditorPreview'; |
||
| 810 | |||
| 811 | return $this->renderWith($templates); |
||
| 812 | } |
||
| 813 | |||
| 814 | /** |
||
| 815 | * @return Member |
||
| 816 | */ |
||
| 817 | public function getAuthor() |
||
| 824 | } |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Get a user defined style variant for this element, if available |
||
| 828 | * |
||
| 829 | * @return string |
||
| 830 | */ |
||
| 831 | public function getStyleVariant() |
||
| 832 | { |
||
| 833 | $style = $this->Style; |
||
| 834 | $styles = $this->config()->get('styles'); |
||
| 835 | |||
| 836 | if (isset($styles[$style])) { |
||
| 837 | $style = strtolower($style); |
||
| 838 | } else { |
||
| 839 | $style = ''; |
||
| 840 | } |
||
| 841 | |||
| 842 | $this->extend('updateStyleVariant', $style); |
||
| 843 | |||
| 844 | return $style; |
||
| 845 | } |
||
| 846 | |||
| 847 | /** |
||
| 848 | * @return mixed|null |
||
| 849 | * @throws \Psr\Container\NotFoundExceptionInterface |
||
| 850 | * @throws \SilverStripe\ORM\ValidationException |
||
| 851 | */ |
||
| 852 | public function getPageTitle() |
||
| 861 | } |
||
| 862 | } |
||
| 863 |