Skip to content

Commit

Permalink
#2766 more work on cf2 file fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Oct 23, 2018
1 parent 9ecc5d6 commit 800f711
Show file tree
Hide file tree
Showing 32 changed files with 3,739 additions and 1,677 deletions.
3 changes: 2 additions & 1 deletion assets/js/frontend-script-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ window.addEventListener("load", function(){
idAttr: form_id,
formId: formId,
state: state,
fieldIds: CFFIELD_CONFIG[instance].fields.hasOwnProperty( 'ids' ) ? CFFIELD_CONFIG[instance].fields.ids : []
fieldIds: CFFIELD_CONFIG[instance].fields.hasOwnProperty( 'ids' ) ? CFFIELD_CONFIG[instance].fields.ids : [],
nonce: jQuery( '#_cf_verify_' + formId ).val()
});


Expand Down
16 changes: 15 additions & 1 deletion caldera-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,18 @@ function caldera_forms_fallback_shortcode()

return esc_html__('Form could not be loaded. Contact the site administrator.', 'caldera-forms');

}
}

/**
* Switch advanced file fields to new type
*
* @since 1.8.0
*
* @TODO move this somewhere smarter
*/
add_filter('caldera_forms_render_get_field', function($field, $form){
if( 'advanced_file' === Caldera_Forms_Field_Util::get_type($field,$form) ){
$field['type'] = \calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType::getCf1Identifier();
}
return $field;
},1,2);
2 changes: 1 addition & 1 deletion cf2/Fields/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace calderawp\calderaforms\cf2\Fields;


class FieldType
abstract class FieldType implements FieldTypeContract
{

}
8 changes: 7 additions & 1 deletion cf2/Fields/FieldTypeContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@

interface FieldTypeContract
{

/**
* Get the field's type
*
* @since 1.8.0
*/
public static function getType();
public static function getCf1Identifier();
}
6 changes: 3 additions & 3 deletions cf2/Fields/FieldTypes/FileFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

use calderawp\calderaforms\cf2\Fields\FieldType;

class TextFieldType extends FieldType
class FileFieldType extends FieldType
{

/** @inheritdoc */
public static function getType()
{
return 'text';
return 'file';
}
/** @inheritdoc */
public static function getCf1Identifier()
{
return 'cf2_text';
return 'cf2_file';
}
}
14 changes: 13 additions & 1 deletion cf2/Fields/FieldTypes/TextFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
namespace calderawp\calderaforms\cf2\Fields\FieldTypes;


class TextFieldType
use calderawp\calderaforms\cf2\Fields\FieldType;

class TextFieldType extends FieldType
{

/** @inheritdoc */
public static function getType()
{
return 'text';
}
/** @inheritdoc */
public static function getCf1Identifier()
{
return 'cf2_text';
}
}
29 changes: 21 additions & 8 deletions cf2/Fields/RenderField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
namespace calderawp\calderaforms\cf2\Fields;


use calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType;
use calderawp\calderaforms\cf2\Fields\FieldTypes\TextFieldType;

class RenderField implements RenderFieldContract
{

Expand Down Expand Up @@ -72,35 +75,45 @@ public function render()
protected function getType()
{
switch ($this->field['type']) {
case 'cf2_file' :
return 'file';
case FileFieldType::getCf1Identifier() :
return FileFieldType::getType();
break;
case 'text2':
case TextFieldType::getCf1Identifier() :
default:
return 'text';
return TextFieldType::getType();
break;
}
}

/** @inheritdoc */
public function data()
{
return [

$data = [
'type' => $this->getType(),
'outterIdAttr' => $this->getOuterIdAttr(),
'fieldId' => $this->field['ID'],
'fieldLabel' => $this->field['label'],
'fieldCaption' => $this->field['caption'],
'fieldPlaceHolder' => '',
'required' => $this->field['required'],
'fieldDefault' => $this->field['config']['default'],
'required' => ! empty($this->field['required'])?true:false,
'fieldDefault' => isset($this->field['config']['default'])? $this->field['config']['default'] : '',
'fieldValue' => '',
'fieldIdAttr' => $this->field['fieldIdAttr'],
'configOptions' => []

];
if( FileFieldType::getType() === $this->getType() ){
$data['configOptions'] = [
'multiple'=> ! empty($this->field[ 'config' ]['multi_upload' ]) ? $this->field[ 'config' ]['multi_upload' ] : false,
'multiUploadText' => ! empty($this->field[ 'config' ]['multi_upload_text' ]) ? $this->field[ 'config' ]['multi_upload_text' ] : false,
'allowedTypes' => ! empty($this->field[ 'config' ]['allowed' ]) ? $this->field[ 'config' ]['allowed' ] : false,
];
}
return $data;
}


/** @inheritdoc */
public function getOuterIdAttr()
{
return sprintf('cf2-%s', $this->getFieldIdAttr());
Expand Down
8 changes: 4 additions & 4 deletions classes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -3935,8 +3935,6 @@ static public function render_field($field, $form = null, $entry_data = array(),
$current_form_count = Caldera_Forms_Render_Util::get_current_form_count();
$type = Caldera_Forms_Field_Util::get_type($field, $form);
$field_id_attr = Caldera_Forms_Field_Util::get_base_id($field, $current_form_count, $form);


if( Caldera_Forms_Field_Util::is_cf2_field_type($type)){
$form_id_attribute = $form['ID'] . '_' . $current_form_count;
$field['required'] = ! empty( $field['required'] ) ? true : false;
Expand Down Expand Up @@ -4507,8 +4505,10 @@ static public function render_form($atts, $entry_id = null, $shortcode = null)


if (empty($field) || !isset($field_types[$field['type']]['file']) || !file_exists($field_types[$field['type']]['file'])) {
continue;
}
if (!Caldera_Forms_Field_Util::is_cf2_field_type($field['type'])) {
continue;
}
}

$field['grid_location'] = $location;

Expand Down
2 changes: 1 addition & 1 deletion classes/field/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,6 @@ public static function is_email_identifying_field($field, array $form )

public static function is_cf2_field_type($field_type){
//This array should be created dynamically
return in_array( $field_type, ['file_cf2', 'text2'] );
return in_array( $field_type, ['cf2_file', 'cf2_text'] );
}
}
19 changes: 7 additions & 12 deletions classes/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,15 @@ public static function internal_types() {
),

),
'text2' => array(


'cf2_text' => array(
"cf2" => true,
"field" => __( 'text2', 'caldera-forms' ),
"description" => __( 'text2', 'caldera-forms' ),
"file" => CFCORE_PATH . "fields/text2/field.php",
"field" => __( 'cf2_text', 'caldera-forms' ),
"description" => __( 'cf2_text', 'caldera-forms' ),
"file" => CFCORE_PATH . "fields/cf2_text/field.php",
"category" => __( 'Basic', 'caldera-forms' ),
"setup" => array(
"template" => CFCORE_PATH . "fields/text2/config.php",
"preview" => CFCORE_PATH . "fields/text2/preview.php"
"template" => CFCORE_PATH . "fields/cf2_text/config.php",
"preview" => CFCORE_PATH . "fields/cf2_text/preview.php"
),
),
'hidden' => array(
Expand Down Expand Up @@ -442,18 +440,15 @@ public static function internal_types() {
),

),
'file_cf2' => array(
'cf2_file' => array(
"cf2" => true,
"field" => __( 'File (CF2)', 'caldera-forms' ),

'icon' => CFCORE_URL . 'assets/build/images/cloud-upload.svg',
"category" => __( 'File', 'caldera-forms' ),
"setup" => array(
"preview" => CFCORE_PATH . "fields/file/preview.php",
"template" => CFCORE_PATH . "fields/file/config_template.php"
)


),

//content
Expand Down
Loading

0 comments on commit 800f711

Please sign in to comment.