Completed
Push — master ( 8c95bb...199f9c )
by Nate
15:05
created

AbstractField::getInputHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 2
1
<?php
2
3
namespace flipbox\hubspot\fields;
4
5
use Craft;
6
use craft\base\Element;
7
use craft\base\ElementInterface;
8
use craft\base\Field;
9
use craft\base\PreviewableFieldInterface;
10
11
abstract class AbstractField extends Field implements PreviewableFieldInterface
12
{
13
14
    const FIELD_TYPE = '';
15
16
    /**
17
     * @inheritdoc
18
     */
19
    public static function displayName(): string
20
    {
21
        return Craft::t('hubspot', 'Hubspot: ' . static::FIELD_TYPE);
22
    }
23
24
    /**
25
     * @param ElementInterface $element
26
     * @return null|string
27
     */
28
    abstract protected function findObjectId(ElementInterface $element);
29
    
30
    /**
31
     * @inheritdoc
32
     */
33
    public static function hasContentColumn(): bool
34
    {
35
        return false;
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public static function supportedTranslationMethods(): array
42
    {
43
        return [
44
            self::TRANSLATION_METHOD_SITE,
45
        ];
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function normalizeValue($value, ElementInterface $element = null)
52
    {
53
54
        if (is_string($value) || is_numeric($value)) {
55
            return $value;
56
        }
57
58
        if (is_null($value) && $element !== null && !empty($element->getId())) {
59
            return $this->findObjectId($element);
60
        }
61
62
        return $value;
63
    }
64
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function getTableAttributeHtml($value, ElementInterface $element): string
70
    {
71
        return is_string($value) ? $value : '';
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function getInputHtml($value, ElementInterface $element = null): string
78
    {
79
        /** @var Element $element */
80
        return Craft::$app->getView()->renderTemplate(
81
            'hubspot/_components/fieldtypes/' . static::FIELD_TYPE . '/input',
82
            [
83
                'name' => $this->handle,
84
                'value' => $value,
85
                'field' => $this,
86
                'hasErrors' => $element->hasErrors($this->handle)
87
            ]
88
        );
89
    }
90
}
91