Skip to content

Commit

Permalink
feat: use TagField for inline linking to elements (fixes #49)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Apr 18, 2023
1 parent 4de1b63 commit a88567b
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 166 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
/resources/
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"DNADesign\\ElementalVirtual\\Tests\\BaseElementExtensionTest::testVirtualElementAnchor":4,"DNADesign\\ElementalVirtual\\Tests\\BaseElementExtensionTest::testUpdateCmsFields":1},"times":{"DNADesign\\ElementalVirtual\\Tests\\BaseElementExtensionTest::testVirtualElementAnchor":0,"DNADesign\\ElementalVirtual\\Tests\\BaseElementExtensionTest::testUpdateCmsFields":0}}
41 changes: 0 additions & 41 deletions .travis.yml

This file was deleted.

21 changes: 21 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Note: Currently chrome latest is recommended
# Behat test setup requires an `.env` with `SS_BASE_URL` defined, as well
# as each of the following commands to initiate a test run:
# ========================================================================= #
# composer require silverstripe/recipe-testing ^1
# vendor/bin/behat-ss @<module>
# ========================================================================= #
default:
suites: []
extensions:
SilverStripe\BehatExtension\MinkExtension:
default_session: facebook_web_driver
javascript_session: facebook_web_driver
facebook_web_driver:
browser: chrome
wd_host: "http://127.0.0.1:9515" #chromedriver port
browser_name: chrome
SilverStripe\BehatExtension\Extension:
bootstrap_file: vendor/silverstripe/cms/tests/behat/serve-bootstrap.php
screenshot_path: %paths.base%/artifacts/screenshots
retry_seconds: 4 # default is 2
1 change: 0 additions & 1 deletion codecov.yml

This file was deleted.

25 changes: 15 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,37 @@
"prefer-stable": true,
"require": {
"dnadesign/silverstripe-elemental": "^3 || ^4",
"tractorcow/silverstripe-autocomplete": "^4",
"silverstripe/tagfield": "^2.0",
"silverstripe/vendor-plugin": "^1.0",
"silverstripe/admin": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^3.0"
"silverstripe/recipe-testing": "^2"
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-main": "1.x-dev"
},
"expose": [
"css"
],
"project-files-installed": [
"behat.yml",
"phpcs.xml.dist",
"phpunit.xml.dist"
]
},
"scripts": {
"lint": "phpcs --extensions=php src/ tests/",
"syntax-check": "find src/ tests/ -type f -name '*.php' -exec php -l {} \\;",
"lint-clean": "phpcbf src/ tests/ --extensions=php",
"remove-uses": "php-cs-fixer fix src/ tests/ --rules=no_unused_imports"
},
"autoload": {
"psr-4": {
"DNADesign\\ElementalVirtual\\": "src/",
"DNADesign\\ElementalVirtual\\Tests\\": "tests/php/"
}
},
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/recipe-plugin": true,
"silverstripe/vendor-plugin": true
}
}
}
24 changes: 24 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<ruleset name="SS4">
<description>Coding standard for SilverStripe 4.x</description>

<!-- Don't sniff third party libraries -->
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/thirdparty/*</exclude-pattern>

<!-- Show progress and output sniff names on violation, and add colours -->
<arg value="sp"/>
<arg name="colors"/>

<!-- Use PSR-2 as a base standard -->
<rule ref="PSR2">
<!-- Allow classes to not declare a namespace -->
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>

<!-- Allow underscores in class names -->
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>

<!-- Allow non camel cased method names -->
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
</rule>
</ruleset>
26 changes: 13 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/php</directory>
</exclude>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<directory suffix=".php">tests/php</directory>
</exclude>
</coverage>
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
</phpunit>
71 changes: 0 additions & 71 deletions src/Forms/ElementalGridFieldAddExistingAutocompleter.php

This file was deleted.

35 changes: 11 additions & 24 deletions src/Model/ElementVirtual.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@

namespace DNADesign\ElementalVirtual\Model;

use TractorCow\AutoComplete\AutoCompleteField;
use SilverStripe\ElementalVirtual\Forms\ElementalGridFieldAddExistingAutocompleter;
use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\TagField\TagField;

/**
* Virtual Linked Element.
*
* As elemental is based on a natural has_one relation to an object,
* this allows the same element to be linked to multiple pages.
*
* {@see ElementalGridFieldAddExistingAutocompleter}
*/
class ElementVirtual extends BaseElement
{
Expand All @@ -36,8 +29,8 @@ class ElementVirtual extends BaseElement
private static $table_name = 'ElementVirtual';

private static $singular_name = 'virtual block';
private static $inline_editable = false;

private static $inline_editable = true;

/**
* @param BaseElement
Expand Down Expand Up @@ -67,29 +60,23 @@ public function getCMSFields()
$fields->addFieldToTab('Root.Main', LiteralField::create('WarningHeader', '<p class="message error">' . $warning . '</p>'));
}

$autocomplete = AutoCompleteField::create(
'LinkedElementID',
_t(__CLASS__ . '.LinkedElement', 'Linked Element'),
'',
BaseElement::class,
'Title'
);

$autocomplete->setLabelField('VirtualLinkedSummary');
$autocomplete->setDisplayField('VirtualLinkedSummary');
$autocomplete->setSourceFilter(['AvailableGlobally' => 1]);
$availableBlocks = BaseElement::get()->filter('AvailableGlobally', 1)->exclude('ClassName', ElementVirtual::class);

$fields->replaceField(
'LinkedElementID',
$autocomplete
TagField::create("LinkedElementID", $this->fieldLabel('LinkedElement'), $availableBlocks)
->setIsMultiple(false)
->setCanCreate(false)
->setShouldLazyLoad(true)
);
if($this->LinkedElementID){

if ($this->LinkedElementID) {
$message = sprintf(
'<p>%s</p><p><a href="%2$s" target="_blank">Click here to edit the original</a></p>',
_t(__CLASS__ . '.VirtualDescription', 'This is a virtual copy of an element.'),
$this->LinkedElement()->getEditLink()
);

$fields->addFieldToTab('Root.Main', LiteralField::create('Existing', $message));
}
});
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion tests/php/BaseElementExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BaseElementExtensionTest extends SapphireTest
TestPage::class
];

public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down

0 comments on commit a88567b

Please sign in to comment.