Actions
1 year ago
Commands
2 years ago
Traits
4 years ago
Validators
4 years ago
resources
3 years ago
templates
3 years ago
AddEnctypeAttributeInDonationForm.php
4 years ago
FieldView.php
3 years ago
FilterCallbackCollection.php
3 years ago
ServiceProvider.php
1 year ago
TemplateHooks.php
4 years ago
UniqueIdAttributeGenerator.php
4 years ago
functions.php
4 years ago
FieldView.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Form\LegacyConsumer; |
| 4 | |
| 5 | use Give\Framework\FieldsAPI\Contracts\Node; |
| 6 | use Give\Framework\FieldsAPI\LegacyNodes\CheckboxGroup; |
| 7 | use Give\Framework\FieldsAPI\Types; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.10.2 |
| 11 | * @since 2.14.0 Add field classes hook for setting custom class names on the wrapper. |
| 12 | */ |
| 13 | class FieldView |
| 14 | { |
| 15 | const INPUT_TYPE_ATTRIBUTES = [ |
| 16 | Types::PHONE => 'tel', |
| 17 | Types::EMAIL => 'email', |
| 18 | Types::URL => 'url', |
| 19 | ]; |
| 20 | |
| 21 | /** |
| 22 | * @since 2.28.0 add shim for CheckboxGroup, only necessary for legacy FFM fields. |
| 23 | * @since 2.10.2 |
| 24 | * @since 2.14.0 add $formId as a param |
| 25 | * @since 2.14.0 Add filter to allow rendering logic for custom fields |
| 26 | * @since 2.16.0 Add visibility conditions to field container |
| 27 | * |
| 28 | * @param Node $field |
| 29 | * @param int $formId |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public static function render(Node $field, $formId) |
| 34 | { |
| 35 | $type = $field->getType(); |
| 36 | $fieldIdAttribute = give(UniqueIdAttributeGenerator::class)->getId($formId, $field->getName()); |
| 37 | |
| 38 | if ($type === Types::HIDDEN) { |
| 39 | include static::getTemplatePath('hidden'); |
| 40 | |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if ($type === CheckboxGroup::TYPE) { |
| 45 | $type = Types::CHECKBOX; |
| 46 | } |
| 47 | |
| 48 | $classList = apply_filters("give_form_{$formId}_field_classes_{$field->getName()}", [ |
| 49 | 'form-row', |
| 50 | 'form-row-wide', |
| 51 | ]); |
| 52 | $className = implode(' ', array_unique($classList)); |
| 53 | |
| 54 | printf( |
| 55 | '<div class="%1$s" data-field-type="%2$s" data-field-name="%3$s" %4$s>', |
| 56 | $className, |
| 57 | $type, |
| 58 | $field->getName(), |
| 59 | self::getVisibilityConditionAttribute($field) |
| 60 | ); |
| 61 | |
| 62 | // By default, new fields will use templates/label.html.php and templates/base.html.php |
| 63 | switch ($type) { |
| 64 | case Types::HTML: |
| 65 | case Types::CHECKBOX: |
| 66 | case Types::RADIO: // Radio provides its own label |
| 67 | include static::getTemplatePath($type); |
| 68 | break; |
| 69 | // These fields need a label and have their own template. |
| 70 | case Types::FILE: |
| 71 | case Types::SELECT: |
| 72 | case Types::TEXTAREA: |
| 73 | include static::getTemplatePath('label'); |
| 74 | include static::getTemplatePath($type); |
| 75 | break; |
| 76 | // By default, include a template and use the base input template. |
| 77 | case Types::DATE: |
| 78 | case Types::EMAIL: |
| 79 | case Types::PHONE: |
| 80 | case Types::TEXT: |
| 81 | case Types::URL: |
| 82 | // Used in the template |
| 83 | $typeAttribute = array_key_exists( |
| 84 | $type, |
| 85 | static::INPUT_TYPE_ATTRIBUTES |
| 86 | ) ? static::INPUT_TYPE_ATTRIBUTES[$type] : 'text'; |
| 87 | include static::getTemplatePath('label'); |
| 88 | include static::getTemplatePath('base'); |
| 89 | break; |
| 90 | default: |
| 91 | /** |
| 92 | * Provide a custom function to render for a custom node type. |
| 93 | * |
| 94 | * @since 2.14.0 |
| 95 | * |
| 96 | * @param Node $field The node to render. |
| 97 | * @param int $formId The form ID that the node is a part of. |
| 98 | * |
| 99 | * @void |
| 100 | */ |
| 101 | do_action("give_fields_api_render_{$field->getType()}", $field, $formId); |
| 102 | } |
| 103 | echo '</div>'; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @since 2.12.0 |
| 108 | * |
| 109 | * @param string $templateName |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | protected static function getTemplatePath($templateName) |
| 114 | { |
| 115 | return plugin_dir_path(__FILE__) . "/templates/{$templateName}.html.php"; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param Node $field |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | private static function getVisibilityConditionAttribute(Node $field) |
| 124 | { |
| 125 | $visibilityConditions = method_exists($field, 'getVisibilityConditions') ? $field->getVisibilityConditions( |
| 126 | ) : null; |
| 127 | |
| 128 | if ($visibilityConditions) { |
| 129 | $visibilityConditionsJson = esc_attr(json_encode($visibilityConditions)); |
| 130 | |
| 131 | return "data-field-visibility-conditions=\"$visibilityConditionsJson\""; |
| 132 | } |
| 133 | |
| 134 | return ''; |
| 135 | } |
| 136 | } |
| 137 |