Actions
4 years ago
Commands
4 years ago
Traits
4 years ago
Validators
4 years ago
resources
4 years ago
templates
4 years ago
AddEnctypeAttributeInDonationForm.php
4 years ago
FieldView.php
4 years ago
FilterCallbackCollection.php
4 years ago
ServiceProvider.php
4 years ago
TemplateHooks.php
4 years ago
UniqueIdAttributeGenerator.php
4 years ago
functions.php
4 years ago
FieldView.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Form\LegacyConsumer; |
| 4 | |
| 5 | use Give\Framework\FieldsAPI\Types; |
| 6 | use Give\Framework\FieldsAPI\Contracts\Node; |
| 7 | |
| 8 | /** |
| 9 | * @since 2.10.2 |
| 10 | * @since 2.14.0 Add field classes hook for setting custom class names on the wrapper. |
| 11 | */ |
| 12 | class FieldView { |
| 13 | const INPUT_TYPE_ATTRIBUTES = [ |
| 14 | Types::PHONE => 'tel', |
| 15 | Types::EMAIL => 'email', |
| 16 | Types::URL => 'url', |
| 17 | ]; |
| 18 | |
| 19 | /** |
| 20 | * @since 2.10.2 |
| 21 | * @since 2.14.0 add $formId as a param |
| 22 | * @since 2.14.0 Add filter to allow rendering logic for custom fields |
| 23 | * |
| 24 | * @param Node $field |
| 25 | * @param int $formId |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public static function render( Node $field, $formId ) { |
| 30 | $type = $field->getType(); |
| 31 | $fieldIdAttribute = give( UniqueIdAttributeGenerator::class )->getId( $formId, $field->getName() ); |
| 32 | |
| 33 | if ( $type === Types::HIDDEN ) { |
| 34 | include static::getTemplatePath( 'hidden' ); |
| 35 | |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $classList = apply_filters( "give_form_{$formId}_field_classes_{$field->getName()}", [ 'form-row', 'form-row-wide' ] ); |
| 40 | $className = implode( ' ', array_unique( $classList ) ); |
| 41 | |
| 42 | echo "<div class=\"{$className}\" data-field-type=\"{$field->getType()}\" data-field-name=\"{$field->getName()}\">"; |
| 43 | // By default, new fields will use templates/label.html.php and templates/base.html.php |
| 44 | switch ( $type ) { |
| 45 | case Types::HTML: |
| 46 | case Types::CHECKBOX: |
| 47 | case Types::RADIO: // Radio provides its own label |
| 48 | include static::getTemplatePath( $type ); |
| 49 | break; |
| 50 | // These fields need a label and have their own template. |
| 51 | case Types::FILE: |
| 52 | case Types::SELECT: |
| 53 | case Types::TEXTAREA: |
| 54 | include static::getTemplatePath( 'label' ); |
| 55 | include static::getTemplatePath( $type ); |
| 56 | break; |
| 57 | // By default, include a template and use the base input template. |
| 58 | case Types::DATE: |
| 59 | case Types::EMAIL: |
| 60 | case Types::PHONE: |
| 61 | case Types::TEXT: |
| 62 | case Types::URL: |
| 63 | // Used in the template |
| 64 | $typeAttribute = array_key_exists( $type, static::INPUT_TYPE_ATTRIBUTES ) ? static::INPUT_TYPE_ATTRIBUTES[ $type ] : 'text'; |
| 65 | include static::getTemplatePath( 'label' ); |
| 66 | include static::getTemplatePath( 'base' ); |
| 67 | break; |
| 68 | default: |
| 69 | /** |
| 70 | * Provide a custom function to render for a custom node type. |
| 71 | * |
| 72 | * @since 2.14.0 |
| 73 | * |
| 74 | * @param Node $field The node to render. |
| 75 | * @param int $formId The form ID that the node is a part of. |
| 76 | * |
| 77 | * @void |
| 78 | */ |
| 79 | do_action( "give_fields_api_render_{$field->getType()}", $field, $formId ); |
| 80 | } |
| 81 | echo '</div>'; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @since 2.12.0 |
| 86 | * |
| 87 | * @param string $templateName |
| 88 | * |
| 89 | * @return string |
| 90 | */ |
| 91 | protected static function getTemplatePath( $templateName ) { |
| 92 | return plugin_dir_path( __FILE__ ) . "/templates/{$templateName}.html.php"; |
| 93 | } |
| 94 | } |
| 95 |