Skip to content

Commit

Permalink
NEW Allow developers to exclude LinkText field
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Feb 12, 2024
1 parent 5076ef1 commit 55cff9d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 7 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ $fields->addFieldsToTab(
);
```

## Excluding the `LinkText` field

Sometimes you might want to have a link which doesn't have text, or for which you handle the text elsewhere. For example you might have a banner with a link, and you only want to use `LinkField` to control where the banner links to.

You can call the `setExcludeLinkTextField()` method to remove the `LinkText` field from the link modal for all links connected to that link field.

```php
$fields->addFieldsToTab(
'Root.Main',
[
MultiLinkField::create('LinkList')
->setExcludeLinkTextField(true),
Link::create('Link')
->setExcludeLinkTextField(true),
],
);
```

## Unversioned links

The `Link` model has the `Versioned` extension applied to it by default. If you wish for links to not be versioned, then remove the extension from the `Link` model in the projects `app/_config.php` file.
Expand Down
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const LinkField = ({
ownerID,
ownerClass,
ownerRelation,
excludeTextField = false,
}) => {
const [data, setData] = useState({});
const [editingID, setEditingID] = useState(0);
Expand Down Expand Up @@ -260,14 +261,14 @@ const LinkField = ({
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
>
<SortableContext
<SortableContext
items={linkIDs}
strategy={verticalListSortingStrategy}
>
{links}
</SortableContext>
</DndContext>
</div>
</div>
}
return <div>{links}</div>
};
Expand Down Expand Up @@ -321,7 +322,7 @@ const LinkField = ({
const saveRecordFirstText = i18n._t('LinkField.SAVE_RECORD_FIRST', 'Cannot add links until the record has been saved');
const links = renderLinks();

return <LinkFieldContext.Provider value={{ ownerID, ownerClass, ownerRelation, actions, loading }}>
return <LinkFieldContext.Provider value={{ ownerID, ownerClass, ownerRelation, actions, loading, excludeTextField }}>
<div className="link-field__container">
{ saveRecordFirst && <div className="link-field__save-record-first">{saveRecordFirstText}</div>}
{ loading && !isSorting && !saveRecordFirst && <Loading containerClass="link-field__loading"/> }
Expand Down Expand Up @@ -359,6 +360,7 @@ LinkField.propTypes = {
ownerID: PropTypes.number.isRequired,
ownerClass: PropTypes.string.isRequired,
ownerRelation: PropTypes.string.isRequired,
excludeTextField: PropTypes.bool,
};

// redux actions loaded into props - used to get toast notifications
Expand Down
5 changes: 4 additions & 1 deletion client/src/components/LinkModal/LinkModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ const buildSchemaUrl = (typeKey, linkID) => {
const parsedURL = url.parse(schemaUrl);
const parsedQs = qs.parse(parsedURL.query);
parsedQs.typeKey = typeKey;
const { ownerID, ownerClass, ownerRelation } = useContext(LinkFieldContext);
const { ownerID, ownerClass, ownerRelation, excludeTextField } = useContext(LinkFieldContext);
parsedQs.ownerID = ownerID;
parsedQs.ownerClass = ownerClass;
parsedQs.ownerRelation = ownerRelation;
if (excludeTextField) {
parsedQs.excludeTextField = true;
}
for (const prop of ['href', 'path', 'pathname']) {
parsedURL[prop] = joinUrlPaths(parsedURL[prop], linkID.toString());
}
Expand Down
1 change: 1 addition & 0 deletions client/src/entwine/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jQuery.entwine('ss', ($) => {
ownerID: inputField.data('owner-id'),
ownerClass: inputField.data('owner-class'),
ownerRelation: inputField.data('owner-relation'),
excludeTextField: inputField.data('exclude-textfield'),
onChange: this.handleChange.bind(this),
isMulti: this.data('is-multi') ?? false,
types: this.data('types') ?? {},
Expand Down
9 changes: 7 additions & 2 deletions src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function linkForm(): Form
}
$operation = 'create';
}
return $this->createLinkForm($link, $operation);
return $this->createLinkForm($link, $operation, (bool) $this->getRequest()->getVar('excludeTextField'));
}

/**
Expand Down Expand Up @@ -319,7 +319,7 @@ public function linkSort()
/**
* Create the Form used to content manage a Link in a modal
*/
private function createLinkForm(Link $link, string $operation): Form
private function createLinkForm(Link $link, string $operation, bool $excludeTextField = false): Form
{
$id = $link->ID;

Expand All @@ -333,6 +333,11 @@ private function createLinkForm(Link $link, string $operation): Form
$ownerClassName = $owner->ClassName;
$ownerRelation = $this->getOwnerRelationFromRequest();

// Remove LinkText if appropriate
if ($excludeTextField) {
$form->Fields()->removeByName('LinkText');
}

// Add hidden form fields for OwnerID, OwnerClass and OwnerRelation
if ($operation === 'create') {
$form->Fields()->push(HiddenField::create('OwnerID')->setValue($ownerID));
Expand Down
2 changes: 2 additions & 0 deletions src/Form/LinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected function getDefaultAttributes(): array
$attributes['data-owner-id'] = $ownerFields['ID'];
$attributes['data-owner-class'] = $ownerFields['Class'];
$attributes['data-owner-relation'] = $ownerFields['Relation'];
$attributes['data-exclude-textfield'] = $this->getExcludeLinkTextField();
return $attributes;
}

Expand All @@ -62,6 +63,7 @@ public function getSchemaDataDefaults()
$data['ownerID'] = $ownerFields['ID'];
$data['ownerClass'] = $ownerFields['Class'];
$data['ownerRelation'] = $ownerFields['Relation'];
$data['excludeTextField'] = $this->getExcludeLinkTextField();
return $data;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Form/MultiLinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getSchemaDataDefaults()
$data['ownerID'] = $ownerFields['ID'];
$data['ownerClass'] = $ownerFields['Class'];
$data['ownerRelation'] = $ownerFields['Relation'];
$data['excludeTextField'] = $this->getExcludeLinkTextField();
return $data;
}

Expand All @@ -70,6 +71,7 @@ protected function getDefaultAttributes(): array
$attributes['data-owner-id'] = $ownerFields['ID'];
$attributes['data-owner-class'] = $ownerFields['Class'];
$attributes['data-owner-relation'] = $ownerFields['Relation'];
$attributes['data-exclude-textfield'] = $this->getExcludeLinkTextField();
return $attributes;
}

Expand Down
13 changes: 13 additions & 0 deletions src/Form/Traits/AllowedLinkClassesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ trait AllowedLinkClassesTrait
{
private array $allowed_types = [];

private bool $excludeLinkTextField = false;

public function setExcludeLinkTextField(bool $include): static
{
$this->excludeLinkTextField = $include;
return $this;
}

public function getExcludeLinkTextField(): bool
{
return $this->excludeLinkTextField;
}

/**
* Set allowed types for LinkField
* @param string[] $types
Expand Down

0 comments on commit 55cff9d

Please sign in to comment.