Skip to content

Commit

Permalink
Add optional aria-label and title fields, resolves #25
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-lenz committed Aug 23, 2018
1 parent 3ddc2c0 commit f33feed
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 10 deletions.
23 changes: 22 additions & 1 deletion src/fields/LinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class LinkField extends Field
*/
public $defaultText = '';

/**
* @var bool
*/
public $enableAriaLabel = false;

/**
* @var bool
*/
public $enableTitle = false;

/**
* @var array
*/
Expand Down Expand Up @@ -90,6 +100,8 @@ public function normalizeValue($value, ElementInterface $element = null) {
'allowCustomText' => $this->allowCustomText,
'allowTarget' => $this->allowTarget,
'defaultText' => $this->defaultText,
'enableAriaLabel' => $this->enableAriaLabel,
'enableTitle' => $this->enableTitle,
'owner' => $element,
];

Expand All @@ -98,16 +110,25 @@ public function normalizeValue($value, ElementInterface $element = null) {
$attr += array_filter(
json_decode($value, true) ?: [],
function ($key) {
return in_array($key, [ 'customText', 'target', 'type', 'value' ]);
return in_array($key, [
'ariaLabel',
'customText',
'target',
'title',
'type',
'value'
]);
},
ARRAY_FILTER_USE_KEY
);

// If it is an array and the field `isCpFormData` is set, we are saving a cp form
} else if (is_array($value) && isset($value['isCpFormData'])) {
$attr += [
'ariaLabel' => $this->enableAriaLabel && isset($value['ariaLabel']) ? $value['ariaLabel'] : null,
'customText' => $this->allowCustomText && isset($value['customText']) ? $value['customText'] : null,
'target' => $this->allowTarget && isset($value['target']) ? $value['target'] : null,
'title' => $this->enableTitle && isset($value['title']) ? $value['title'] : null,
'type' => isset($value['type']) ? $value['type'] : null,
'value' => $this->getLinkValue($value)
];
Expand Down
49 changes: 47 additions & 2 deletions src/models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ class Link extends Model
public $allowTarget;

/**
* @var string
* @var string|null
*/
public $ariaLabel;

/**
* @var string|null
*/
public $customText;

Expand All @@ -35,11 +40,26 @@ class Link extends Model
*/
public $defaultText;

/**
* @var bool
*/
public $enableAriaLabel;

/**
* @var bool
*/
public $enableTitle;

/**
* @var string
*/
public $target;

/**
* @var string|null
*/
public $title;

/**
* @var string
*/
Expand Down Expand Up @@ -67,6 +87,13 @@ public function __construct($config = []) {
parent::__construct($config);
}

/**
* @return null|string
*/
public function getAriaLabel() {
return $this->ariaLabel;
}

/**
* @return null|\craft\base\ElementInterface
*/
Expand Down Expand Up @@ -104,11 +131,22 @@ public function getLink($attributesOrText = null) {
}

$attr = [ 'href' => $url ];

$ariaLabel = $this->getAriaLabel();
if (!empty($ariaLabel)) {
$attr['arial-label'] = $ariaLabel;
}

$target = $this->getTarget();
if (!is_null($target)) {
if (!empty($target)) {
$attr['target'] = $target;
}

$title = $this->getTitle();
if (!empty($title)) {
$attr['title'] = $title;
}

// If a string is passed, override the text component
if (is_string($attributesOrText)) {
$text = $attributesOrText;
Expand Down Expand Up @@ -183,6 +221,13 @@ public function getText() {
return \Craft::t('site', $this->defaultText);
}

/**
* @return null|string
*/
public function getTitle() {
return $this->title;
}

/**
* Try to use provided custom text or field default.
* Allows user to specify a fallback string if the custom text and default are not set.
Expand Down
32 changes: 25 additions & 7 deletions src/templates/_input.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@

{% if hasSettings %}
<div class="linkfield--settings{{ type == '' ? ' hidden' }}">
{% if settings.allowTarget %}
{{ forms.checkboxField({
id: name~'-Target',
name: name~'[target]',
value: '_blank',
label: 'Open link in new window?'|t('typedlinkfield'),
checked: value.target is defined and value.target == '_blank' ? true : null
}) }}
{% endif %}

{% if settings.allowCustomText %}
{{ forms.textField({
id: name~'-customText',
Expand All @@ -40,13 +50,21 @@
}) }}
{% endif %}

{% if settings.allowTarget %}
{{ forms.checkboxField({
id: name~'-Target',
name: name~'[target]',
value: '_blank',
label: 'Open link in new window?'|t('typedlinkfield'),
checked: value.target is defined and value.target == '_blank' ? true : null
{% if settings.enableAriaLabel %}
{{ forms.textField({
id: name~'-AriaLabel',
name: name~'[ariaLabel]',
label: 'Aria label'|t('typedlinkfield'),
value: value.ariaLabel is defined ? value.ariaLabel : ""
}) }}
{% endif %}

{% if settings.enableTitle %}
{{ forms.textField({
id: name~'-Title',
name: name~'[title]',
label: 'Title'|t('typedlinkfield'),
value: value.title is defined ? value.title : ""
}) }}
{% endif %}
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/templates/_settings-general.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@
name: 'allowTarget',
checked: settings.allowTarget
}) }}

{{ forms.checkboxField({
label: 'Enable aria label support'|t('typedlinkfield'),
name: 'enableAriaLabel',
checked: settings.enableAriaLabel
}) }}

{{ forms.checkboxField({
label: 'Enable title support'|t('typedlinkfield'),
name: 'enableTitle',
checked: settings.enableTitle
}) }}

0 comments on commit f33feed

Please sign in to comment.