| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Form\LegacyConsumer; |
| 4 |
|
| 5 |
use Give\Framework\FieldsAPI\Contracts\Node; |
| 6 |
use Give\Framework\FieldsAPI\Types; |
| 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 |
* @since 2.16.0 Add visibility conditions to field container |
| 24 |
* |
| 25 |
* @param Node $field |
| 26 |
* @param int $formId |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public static function render( Node $field, $formId ) { |
| 31 |
$type = $field->getType(); |
| 32 |
$fieldIdAttribute = give( UniqueIdAttributeGenerator::class )->getId( $formId, $field->getName() ); |
| 33 |
|
| 34 |
if ( $type === Types::HIDDEN ) { |
| 35 |
include static::getTemplatePath( 'hidden' ); |
| 36 |
|
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
$classList = apply_filters( "give_form_{$formId}_field_classes_{$field->getName()}", [ |
| 41 |
'form-row', |
| 42 |
'form-row-wide' |
| 43 |
] ); |
| 44 |
$className = implode( ' ', array_unique( $classList ) ); |
| 45 |
|
| 46 |
printf( |
| 47 |
'<div class="%1$s" data-field-type="%2$s" data-field-name="%3$s" %4$s>', |
| 48 |
$className, |
| 49 |
$field->getType(), |
| 50 |
$field->getName(), |
| 51 |
self::getVisibilityConditionAttribute( $field ) |
| 52 |
); |
| 53 |
|
| 54 |
// By default, new fields will use templates/label.html.php and templates/base.html.php |
| 55 |
switch ( $type ) { |
| 56 |
case Types::HTML: |
| 57 |
case Types::CHECKBOX: |
| 58 |
case Types::RADIO: // Radio provides its own label |
| 59 |
include static::getTemplatePath( $type ); |
| 60 |
break; |
| 61 |
// These fields need a label and have their own template. |
| 62 |
case Types::FILE: |
| 63 |
case Types::SELECT: |
| 64 |
case Types::TEXTAREA: |
| 65 |
include static::getTemplatePath( 'label' ); |
| 66 |
include static::getTemplatePath( $type ); |
| 67 |
break; |
| 68 |
// By default, include a template and use the base input template. |
| 69 |
case Types::DATE: |
| 70 |
case Types::EMAIL: |
| 71 |
case Types::PHONE: |
| 72 |
case Types::TEXT: |
| 73 |
case Types::URL: |
| 74 |
// Used in the template |
| 75 |
$typeAttribute = array_key_exists( $type, static::INPUT_TYPE_ATTRIBUTES ) ? static::INPUT_TYPE_ATTRIBUTES[ $type ] : 'text'; |
| 76 |
include static::getTemplatePath( 'label' ); |
| 77 |
include static::getTemplatePath( 'base' ); |
| 78 |
break; |
| 79 |
default: |
| 80 |
/** |
| 81 |
* Provide a custom function to render for a custom node type. |
| 82 |
* |
| 83 |
* @since 2.14.0 |
| 84 |
* |
| 85 |
* @param Node $field The node to render. |
| 86 |
* @param int $formId The form ID that the node is a part of. |
| 87 |
* |
| 88 |
* @void |
| 89 |
*/ |
| 90 |
do_action( "give_fields_api_render_{$field->getType()}", $field, $formId ); |
| 91 |
} |
| 92 |
echo '</div>'; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @since 2.12.0 |
| 97 |
* |
| 98 |
* @param string $templateName |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
protected static function getTemplatePath( $templateName ) { |
| 103 |
return plugin_dir_path( __FILE__ ) . "/templates/{$templateName}.html.php"; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @param Node $field |
| 108 |
* |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
private static function getVisibilityConditionAttribute( Node $field ) { |
| 112 |
$visibilityConditions = method_exists( $field, 'getVisibilityConditions' ) ? $field->getVisibilityConditions() : null; |
| 113 |
|
| 114 |
if ( $visibilityConditions ) { |
| 115 |
$visibilityConditionsJson = esc_attr( json_encode( $visibilityConditions ) ); |
| 116 |
|
| 117 |
return "data-field-visibility-conditions=\"$visibilityConditionsJson\""; |
| 118 |
} |
| 119 |
|
| 120 |
return ''; |
| 121 |
} |
| 122 |
} |
| 123 |
|