Skip to content

Commit

Permalink
Makes LinkMate field previewable in element indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed Feb 4, 2023
1 parent cbe715b commit 8df42fa
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions src/fields/LinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\PreviewableFieldInterface;
use craft\helpers\Html;
use craft\helpers\Json;
use Exception;
use Throwable;

use craft\helpers\StringHelper;
use craft\helpers\UrlHelper;
use vaersaagod\linkmate\helpers\MigrateHelper;
use vaersaagod\linkmate\LinkMate;
use vaersaagod\linkmate\models\Link;
use vaersaagod\linkmate\models\LinkTypeInterface;
use vaersaagod\linkmate\validators\LinkFieldValidator;

use yii\db\Schema;

/**
* Class LinkField
*
* @package vaersaagod\linkmate\fields
*
* @property-read array[] $elementValidationRules
* @property-read string $contentColumnType
* @property-read array[] $elementValidationRules
* @property-read string $contentColumnType
* @property-read LinkTypeInterface[] $allowedLinkTypes
* @property-read string $settingsHtml
* @property-read string $settingsHtml
*/
class LinkField extends Field
class LinkField extends Field implements PreviewableFieldInterface
{

/**
Expand Down Expand Up @@ -128,7 +132,7 @@ public function getContentColumnType(): string
}

/**
* @param mixed $value
* @param mixed $value
* @param ElementInterface|null $element
*
* @return mixed
Expand All @@ -151,17 +155,17 @@ public function normalizeValue(mixed $value, ?ElementInterface $element = null):
if (is_array($decodedValue)) {
$attr += $decodedValue;
}
} catch (Exception) {
} catch (\Throwable) {
}
} else if (is_array($value) && isset($value['isCpFormData'])) {
// If it is an array and the field `isCpFormData` is set, we are saving a cp form
$attr += [
'ariaLabel' => $this->enableAriaLabel && isset($value['ariaLabel']) ? $value['ariaLabel'] : null,
'customQuery' => isset($value['customQuery']) ? $value['customQuery'] : null,
'customQuery' => $value['customQuery'] ?? 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,
'type' => $value['type'] ?? null,
'value' => $this->getLinkValue($value)
];
} else if (is_array($value)) {
Expand Down Expand Up @@ -229,11 +233,13 @@ public function getElementValidationRules(): array
}

/**
* @param Link $value
* @param $value
* @param ElementInterface|null $element
*
* @return string
* @throws Throwable
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @throws \yii\base\Exception
*/
public function getInputHtml($value, ElementInterface $element = null): string
{
Expand Down Expand Up @@ -276,7 +282,7 @@ public function getInputHtml($value, ElementInterface $element = null): string
}

/**
* @param string $linkTypeName
* @param string $linkTypeName
* @param LinkTypeInterface $linkType
*
* @return array
Expand All @@ -296,7 +302,10 @@ public function getLinkTypeSettings(string $linkTypeName, LinkTypeInterface $lin

/**
* @return string
* @throws Throwable
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @throws \yii\base\Exception
*/
public function getSettingsHtml(): string
{
Expand Down Expand Up @@ -428,4 +437,23 @@ public static function displayName(): string
{
return Craft::t('linkmate', 'LinkMate field');
}

/**
* @param mixed $value
* @param ElementInterface $element
* @return string
*/
public function getTableAttributeHtml(mixed $value, ElementInterface $element): string
{
$url = (string)$value;
if (!$url) {
return '';
}
$url = Html::encode($url);
if (!UrlHelper::isFullUrl($url) && !str_starts_with($url, 'mailto:') && !str_starts_with($url, 'tel:')) {
return $url;
}
$label = StringHelper::truncate($url, 50);
return "<a href=\"$url\" target=\"_blank\" class=\"go\">$label</a>";
}
}

0 comments on commit 8df42fa

Please sign in to comment.