Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
URL Field
Browse files Browse the repository at this point in the history
  • Loading branch information
asedano committed Sep 27, 2018
1 parent 60a257e commit 1410ca4
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
49 changes: 49 additions & 0 deletions assets/acf_5/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,52 @@ acf.registerFieldType(acf.models.WysiwygField.extend({
});
}
}));

acf.registerFieldType(acf.models.UrlField.extend({
type: 'qtranslate_url',
$control: function(){
return this.$('.acf-input-wrap.current-language');
},

$input: function(){
return this.$('.acf-input-wrap.current-language input[type="url"]');
},
isValid: function(){

// vars
var val = this.val();

// bail early if no val
if( !val ) {
return false;
}

// url
if( val.indexOf('://') !== -1 ) {
return true;
}

// protocol relative url
if( val.indexOf('//') === 0 ) {
return true;
}

// relative url
if( val.indexOf('/') === 0 ) {
return true;
}

// return
return false;
},

render: function(){

// add class
if( this.isValid() ) {
this.$control().addClass('-valid');
} else {
this.$control().removeClass('-valid');
}
},
}));
5 changes: 5 additions & 0 deletions assets/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
.multi-language-field > textarea,
.multi-language-field > fieldset,
.multi-language-field .acf_wysiwyg,
.multi-language-field .acf-url,
.multi-language-field .acf-editor-wrap,
.multi-language-field .acf-file-uploader,
.multi-language-field .acf-image-uploader { display: none }
Expand All @@ -79,6 +80,10 @@
margin-top: 0;
}

.acf-input .acf-url input[type="text"] {
padding-left: 25px;
}

.multi-language-field .acf-file-uploader,
.multi-language-field .acf-image-uploader {
padding: 20px 10px 10px;
Expand Down
2 changes: 2 additions & 0 deletions src/acf_5/acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public function include_fields() {
require_once ACF_QTRANSLATE_PLUGIN_DIR . 'src/acf_5/fields/text.php';
require_once ACF_QTRANSLATE_PLUGIN_DIR . 'src/acf_5/fields/textarea.php';
require_once ACF_QTRANSLATE_PLUGIN_DIR . 'src/acf_5/fields/wysiwyg.php';
require_once ACF_QTRANSLATE_PLUGIN_DIR . 'src/acf_5/fields/url.php';

acf()->fields->register_field_type(new acf_qtranslate_acf_5_text($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_textarea($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_wysiwyg($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_image($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_file($this->plugin));
acf()->fields->register_field_type(new acf_qtranslate_acf_5_url($this->plugin));
}

/**
Expand Down
124 changes: 124 additions & 0 deletions src/acf_5/fields/url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

class acf_qtranslate_acf_5_url extends acf_qtranslate_acf_5_text {
function initialize() {

// vars
$this->name = 'qtranslate_url';
$this->label = __("Url",'acf');
$this->category = __("qTranslate",'acf');
$this->defaults = array(
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => ''
);

}

/*
* render_field()
*
* Create the HTML interface for your field
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function render_field($field) {
global $q_config;
$languages = qtrans_getSortedLanguages(true);
$values = qtrans_split($field['value'], $quicktags = true);
$currentLanguage = $this->plugin->get_active_language();

// vars
$o = array( 'type', 'id', 'class', 'name', 'value', 'placeholder' );
$s = array( 'readonly', 'disabled' );
$e = '';

// maxlength
if( $field['maxlength'] !== "" ) {
$o[] = 'maxlength';
}

// populate atts
$atts = array();
foreach( $o as $k ) {
$atts[ $k ] = $field[ $k ];
}

// special atts
foreach( $s as $k ) {
if( isset($field[ $k ]) && $field[ $k ] ) {
$atts[ $k ] = $k;
}
}

// render
$e .= '<div class="acf-url-wrap multi-language-field">';

foreach ($languages as $language) {
$class = ($language === $currentLanguage) ? 'wp-switch-editor current-language' : 'wp-switch-editor';
$e .= '<a class="' . $class . '" data-language="' . $language . '">' . $q_config['language_name'][$language] . '</a>';
}

foreach ($languages as $language) {
$atts['class'] = $field['class'];

$atts['type'] = 'text';
$atts['name'] = $field['name'] . "[$language]";
$atts['value'] = $values[$language];

$container_class = 'acf-input-wrap acf-url';
if ($language === $currentLanguage) {
$container_class .= ' current-language';
}

$e .= '<div class="' . $container_class . '" data-language="' . $language . '">';
$e .= '<i class="acf-icon -globe -small"></i>';
$e .= '<input ' . acf_esc_attr( $atts ) . ' />';
$e .= '</div>';

}

$e .= '</div>';

// return
echo $e;
}

function validate_value( $valid, $value, $field, $input ){
foreach ($value as $valor) {
// bail early if empty
if ( empty( $valor ) ) {

continue;

}

if ( strpos( $valor, '://' ) !== false ) {

// url

} elseif ( strpos( $valor, '//' ) === 0 ) {

// protocol relative url

} elseif ( strpos( $valor, '/' ) === 0 ) {

// relative url

} else {

$valid = __( 'Value must be a valid URL', 'acf' );

}
}

// return
return $valid;
}
}

0 comments on commit 1410ca4

Please sign in to comment.