public static function preProcess($value, $settings, $model) { if (is_array($value)) { if (!empty($model)) { $model->changed = true; } if (isset($value['href']) && is_numeric($value['href'])) { $url = \CMF\Model\URL::find(intval($value['href'])); if (!empty($url)) { $value = new \CMF\Model\URL(); $value->populate(array('alias' => $url)); } } else { if (isset($value['external']) && intval($value['external'])) { $url = $model->get($settings['mapping']['fieldName']); if (empty($url)) { $url = new \CMF\Model\URL(); } $url->populate(array('url' => @$value['href'], 'slug' => '', 'prefix' => '', 'item_id' => null, 'type' => \CMF\Model\URL::TYPE_EXTERNAL, 'alias' => null)); return $url; } } } return $value; }
/** inheritdoc */ public static function displayForm($value, &$settings, $model) { $required = isset($settings['required']) ? $settings['required'] : false; $errors = $model->getErrorsForField($settings['mapping']['fieldName']); $has_errors = count($errors) > 0; $input_attributes = array('class' => 'input input-xxlarge'); $attributes = array('class' => 'field-type-link controls control-group' . ($has_errors ? ' error' : '')); $href_name = $settings['mapping']['fieldName'] . '[href]'; $value['href'] = isset($value['href']) ? $value['href'] : null; $label_text = $settings['title'] . ($required ? ' *' : ''); // Translation? if (\CMF::$lang_enabled && !\CMF::langIsDefault() && isset($settings['mapping']['columnName']) && $model->isTranslatable($settings['mapping']['columnName'])) { // If there is no translation if (!$model->hasTranslation($settings['mapping']['columnName'])) { $attributes['class'] .= ' no-translation'; $input_attributes['class'] .= ' no-translation'; $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::defaultLang() . '.png') . '" /> ' . $label_text; } else { $label_text = '<img class="lang-flag" src="' . \Uri::create('/admin/assets/img/lang/' . \CMF::lang() . '.png') . '" /> ' . $label_text; } } // EXTERNAL CHECKBOX $external_name = $settings['mapping']['fieldName'] . '[external]'; $external_value = \Arr::get($value, 'external', false); $external = \Form::hidden($external_name, '0') . html_tag('label', array('class' => 'checkbox external-checkbox'), \Form::checkbox($external_name, '1', $external_value, array()) . ' custom'); $label = \Form::label($label_text . ($has_errors ? ' - ' . $errors[0] : ''), $href_name, array('class' => 'item-label')) . $external . html_tag('div', array('class' => 'clear'), ' '); if ($external_value) { $attributes['class'] .= ' external'; } // EXTERNAL INPUT CONTENT $href_value_ext = $external_value ? $value['href'] : ''; $ext_input = \Form::input($href_name, $href_value_ext, $input_attributes); $ext_content = html_tag('div', array('class' => 'external-link'), $ext_input); // INTERNAL DROPDOWN CONTENT $options = static::getOptions($settings, $model); $href_value_int = $external_value ? '' : $value['href']; // Check if the value is actually an alias if (!empty($href_value_int)) { $url_value = \CMF\Model\URL::find($href_value_int); if ($url_value && ($alias = $url_value->alias)) { $href_value_int = $alias->id; } } $input = \Form::select($href_name, $href_value_int, $options, $input_attributes); $int_content = html_tag('div', array('class' => 'internal-link'), $input); return html_tag('div', $attributes, $label . $int_content . $ext_content) . html_tag('div', array(), ''); }