Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update input placeholders so it can handle translations #56

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 100 additions & 86 deletions fruitlinkit/fieldtypes/FruitLinkItFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public function defineContentAttribute()
public function getSettingsHtml()
{
return craft()->templates->render('fruitlinkit/_fieldtype/settings', array(
'settings' => $this->getSettings(),
'types' => $this->_getAvaiableLinkItTypes(),
'elementSources' => craft()->fruitLinkIt->getLinkItElementSources(),
'thirdPartyElementTypes' => craft()->fruitLinkIt->getThirdPartyElementTypes(),
'settings' => $this->getSettings(),
'types' => $this->_getAvaiableLinkItTypes(),
'elementSources' => craft()->fruitLinkIt->getLinkItElementSources(),
'thirdPartyElementTypes' => craft()->fruitLinkIt->getThirdPartyElementTypes(),
));
}

Expand All @@ -33,24 +33,24 @@ public function getInputHtml($name, $value)
craft()->templates->includeJsResource('fruitlinkit/js/FruitLinkIt.js');
craft()->templates->includeJs('new FruitLinkIt("'.craft()->templates->namespaceInputId($id).'");');

// Settings
$settings = $this->getSettings();

// LinkIt Types
$availableTypes = $this->_getLinkItTypes();
$types = array('' => Craft::t('Link To...'));

if(is_array($settings['types']))
{
foreach($settings['types'] as $type)
{
$types[$type] = $availableTypes[$type];
}
}
else
{
$types = $types + $availableTypes;
}
// Settings
$settings = $this->getSettings();

// LinkIt Types
$availableTypes = $this->_getLinkItTypes();
$types = array('' => Craft::t('Link To...'));

if(is_array($settings['types']))
{
foreach($settings['types'] as $type)
{
$types[$type] = $availableTypes[$type];
}
}
else
{
$types = $types + $availableTypes;
}

// Element Select Options
$elementSelectSettings = array(
Expand All @@ -60,6 +60,7 @@ public function getInputHtml($name, $value)
'sources' => $settings->entrySources,
'criteria' => array(
'status' => null,
'locale' => $this->getTargetLocale(),
),
'sourceElementId' => ( isset($this->element->id) ? $this->element->id : null ),
'limit' => 1,
Expand Down Expand Up @@ -108,23 +109,23 @@ public function getInputHtml($name, $value)
$thirdPartyElementTypes = craft()->fruitLinkIt->getThirdPartyElementTypes();

foreach ($thirdPartyElementTypes as $elementTypeHandle => $elementTypeConfig) {
$data = false;
if ($value) {
$data = $value->getThirdPartyElementData($elementTypeHandle);
}

$elementSelectSettings[$elementTypeHandle] = array(
'elementType' => new ElementTypeVariable( craft()->elements->getElementType($elementTypeConfig['elementType']) ),
'elements' => $value && $data && $data['element'] ? array($data['element']) : null,
'sources' => $settings[$elementTypeHandle.'Sources'],
'criteria' => array(
'status' => null,
),
'sourceElementId' => ( isset($this->element->id) ? $this->element->id : null ),
'limit' => 1,
'addButtonLabel' => Craft::t($settings[$elementTypeHandle.'SelectionLabel']),
'storageKey' => 'field.'.$this->model->id
);
$data = false;
if ($value) {
$data = $value->getThirdPartyElementData($elementTypeHandle);
}

$elementSelectSettings[$elementTypeHandle] = array(
'elementType' => new ElementTypeVariable( craft()->elements->getElementType($elementTypeConfig['elementType']) ),
'elements' => $value && $data && $data['element'] ? array($data['element']) : null,
'sources' => $settings[$elementTypeHandle.'Sources'],
'criteria' => array(
'status' => null,
),
'sourceElementId' => ( isset($this->element->id) ? $this->element->id : null ),
'limit' => 1,
'addButtonLabel' => Craft::t($settings[$elementTypeHandle.'SelectionLabel']),
'storageKey' => 'field.'.$this->model->id
);
}

// Render Field
Expand All @@ -137,45 +138,58 @@ public function getInputHtml($name, $value)
));
}

/**
* Returns the input value as it should be saved to the database.
*
* @param mixed $value
* @return mixed
*/
public function prepValueFromPost($value)
{
/**
* Returns the input value as it should be saved to the database.
*
* @param mixed $value
* @return mixed
*/
public function prepValueFromPost($value)
{
if( is_array($value) && $value['type'] != '' )
{
return json_encode($value);
return json_encode($value);
}
else
{
return '';
}
}

/**
* Preps the field value for use.
*
* @param mixed $value
* @return mixed
*/
public function prepValue($value)
{
}

/**
* Preps the field value for use.
*
* @param mixed $value
* @return mixed
*/
public function prepValue($value)
{
return $this->_valueToModel($value);
}
}

public function validate($value)
{
public function validate($value)
{
if(is_array($value) && $value['type'] != '')
{
$linkModel = $this->_valueToModel($value);
$validated = $linkModel->validate();
return $validated ? true : $linkModel->getAllErrors();
}
parent::validate($value);
}
}

protected function getTargetLocale()
{
if (craft()->isLocalized())
{
if (isset($this->element))
{
return $this->element->locale;
}
}

return craft()->getLanguage();
}


// Protected Methods
Expand All @@ -191,7 +205,7 @@ protected function getSettingsModel()
// =========================================================================

private function _getAvaiableLinkItTypes()
{
{
$types = $this->_getLinkItTypes();
$sources = craft()->fruitLinkIt->getLinkItElementSources();
if(!$sources['entry'])
Expand All @@ -211,34 +225,34 @@ private function _getAvaiableLinkItTypes()
unset($types['product']);
}
return $types;
}
}

private function _getLinkItTypes()
{
$types = array(
'email' => Craft::t('Email Address'),
'tel' => Craft::t('Phone Number'),
'custom' => Craft::t('Custom URL'),
'entry' => Craft::t('Entry'),
'category' => Craft::t('Category'),
'asset' => Craft::t('Asset'),
'product' => Craft::t('Product'),
);

// Give plugins a chance to add their own element types
$thirdPartyElementTypes = craft()->fruitLinkIt->getThirdPartyElementTypes();
foreach ($thirdPartyElementTypes as $elementTypeHandle => $elementTypeConfig)
{
$types = array_merge($types, array(
$elementTypeHandle => $elementTypeConfig['name']
));
}
$types = array(
'email' => Craft::t('Email Address'),
'tel' => Craft::t('Phone Number'),
'custom' => Craft::t('Custom URL'),
'entry' => Craft::t('Entry'),
'category' => Craft::t('Category'),
'asset' => Craft::t('Asset'),
'product' => Craft::t('Product'),
);

return $types;
}
// Give plugins a chance to add their own element types
$thirdPartyElementTypes = craft()->fruitLinkIt->getThirdPartyElementTypes();
foreach ($thirdPartyElementTypes as $elementTypeHandle => $elementTypeConfig)
{
$types = array_merge($types, array(
$elementTypeHandle => $elementTypeConfig['name']
));
}

return $types;
}

private function _valueToModel($value, $settings = false)
{
{
if( is_array($value) && $value['type'] != '' )
{
$settings = $settings ? $settings : $this->getSettings();
Expand All @@ -258,7 +272,7 @@ private function _valueToModel($value, $settings = false)
}

return '';
}
}

private function _prepLinkItValueArray(array $value)
{
Expand Down
13 changes: 7 additions & 6 deletions fruitlinkit/templates/_fieldtype/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
id: name~'Email',
class: name~'Email',
name: name~'[email]',
placeholder: 'Email Address',
placeholder: 'Email Address'|t,
value: type == 'email' ? value.value
}) }}
</div>
Expand All @@ -45,7 +45,7 @@
id: name~'Tel',
class: name~'Tel',
name: name~'[tel]',
placeholder: 'Telephone Number',
placeholder: 'Telephone Number'|t,
value: type == 'tel' ? value.value
}) }}
</div>
Expand All @@ -57,7 +57,7 @@
id: name~'Custom',
class: name~'Custom',
name: name~'[custom]',
placeholder: 'Custom URL',
placeholder: 'Custom URL'|t,
value: type == 'custom' ? value.value
}) }}
</div>
Expand Down Expand Up @@ -120,15 +120,15 @@
{# Text & Target #}
{% if settings.allowCustomText or settings.allowTarget %}
<div class="fruitlinkit-settings{{ type == '' ? ' hidden' }}">

{% if settings.allowCustomText %}

<div class="fruit-field-column fruitlinkit-text">
<div></div>
{{ forms.textField({
id: name~'CustomText',
class: name~'CustomText',
name: name~'[customText]',
placeholder: settings.defaultText == '' ? 'Custom Link Text' : settings.defaultText,
placeholder: settings.defaultText == '' ? 'Custom Link Text'|t : settings.defaultText,
value: value.customText is defined and value.customText ? value.customText
}) }}
</div>
Expand All @@ -138,12 +138,13 @@
{% if settings.allowTarget %}

<div class="fruit-field-column fruitlinkit-target">
<input type="hidden" name="{{ name }}[target]" value="_self" />
{{ forms.checkboxField({
id: name~'Target',
class: name~'Target',
name: name~'[target]',
value: '_blank',
label: 'Open link in new window?',
label: 'Open link in new window?'|t,
checked: value.target is defined and value.target == '_blank' ? true : null
}) }}
</div>
Expand Down
15 changes: 15 additions & 0 deletions fruitlinkit/translations/de.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [
'Email Address' => 'E-Mail Adresse',
'Phone Number' => 'Telefonnummer',
'Custom URL' => 'Beliebige URL',
'Entry' => 'Eintrag',
'Asset' => 'Datei',
'Select an asset' => 'Eine Datei auswählen',
'Select an entry' => 'Einen Eintrag auswählen',
'Select a category' => 'Eine Kateogrie auswählen',
'Custom Link Text' => 'Beliebiger Linktext',
'Open link in new window?' => 'Link im neuen Fenster öffnen?',

];