nystudio107 /
craft-routemap
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Route Map plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * Returns a list of public routes for elements with URLs |
||
| 6 | * |
||
| 7 | * @link https://nystudio107.com/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2017 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\routemap\helpers; |
||
| 12 | |||
| 13 | use craft\base\Component; |
||
| 14 | use craft\base\ElementInterface; |
||
|
0 ignored issues
–
show
The type
craft\base\ElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 15 | use craft\base\Field as BaseField; |
||
| 16 | use craft\elements\Entry; |
||
| 17 | use craft\models\FieldLayout; |
||
| 18 | use yii\base\InvalidConfigException; |
||
| 19 | |||
| 20 | /** |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 22 | * @package RouteMap |
||
|
0 ignored issues
–
show
|
|||
| 23 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 24 | */ |
||
|
0 ignored issues
–
show
|
|||
| 25 | class Field extends Component |
||
| 26 | { |
||
| 27 | // Static Methods |
||
| 28 | // ========================================================================= |
||
| 29 | /** |
||
| 30 | * Return all the fields in the $element of the type $fieldType class |
||
| 31 | * |
||
| 32 | * @param ElementInterface $element |
||
|
0 ignored issues
–
show
|
|||
| 33 | * @param string $fieldType |
||
|
0 ignored issues
–
show
|
|||
| 34 | * @return array |
||
|
0 ignored issues
–
show
|
|||
| 35 | */ |
||
| 36 | public static function fieldsOfType(ElementInterface $element, string $fieldType): array |
||
| 37 | { |
||
| 38 | $foundFields = []; |
||
| 39 | |||
| 40 | $layout = $element->getFieldLayout(); |
||
| 41 | if (!$layout instanceof FieldLayout) { |
||
| 42 | return []; |
||
| 43 | } |
||
| 44 | |||
| 45 | $fields = $layout->getCustomFields(); |
||
| 46 | /** @var BaseField $field */ |
||
|
0 ignored issues
–
show
|
|||
| 47 | foreach ($fields as $field) { |
||
| 48 | if ($field instanceof $fieldType) { |
||
| 49 | $foundFields[] = $field->handle; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | return $foundFields; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Return all the fields in the $matrixBlock of the type $fieldType class |
||
| 58 | * |
||
| 59 | * @param Entry $matrixEntry |
||
|
0 ignored issues
–
show
|
|||
| 60 | * @param string $fieldType |
||
|
0 ignored issues
–
show
|
|||
| 61 | * @return ?array |
||
|
0 ignored issues
–
show
|
|||
| 62 | */ |
||
| 63 | public static function matrixFieldsOfType(Entry $matrixEntry, string $fieldType): ?array |
||
| 64 | { |
||
| 65 | $foundFields = []; |
||
| 66 | |||
| 67 | try { |
||
| 68 | $matrixEntryTypeModel = $matrixEntry->getType(); |
||
| 69 | } catch (InvalidConfigException $e) { |
||
| 70 | $matrixEntryTypeModel = null; |
||
| 71 | } |
||
| 72 | if ($matrixEntryTypeModel) { |
||
| 73 | $fields = $matrixEntryTypeModel->getCustomFields(); |
||
| 74 | /** @var BaseField $field */ |
||
|
0 ignored issues
–
show
|
|||
| 75 | foreach ($fields as $field) { |
||
| 76 | if ($field instanceof $fieldType) { |
||
| 77 | $foundFields[$field->handle] = $field->name; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | return $foundFields; |
||
| 83 | } |
||
| 84 | } |
||
| 85 |