diff --git a/linkit.module b/linkit.module index 78f7704..006871a 100644 --- a/linkit.module +++ b/linkit.module @@ -50,6 +50,11 @@ define('LINKIT_PROFILE_TYPE_EDITOR', 1); */ define('LINKIT_PROFILE_TYPE_FIELD', 2); +/** + * Define the profile type menu. + */ +define('LINKIT_PROFILE_TYPE_MENU', 3); + // Include the file with the field functions. require_once dirname(__FILE__) . '/linkit.field.inc'; @@ -169,6 +174,22 @@ function linkit_profile_editor_load_all() { } /** + * Load all Linkit profiles that is for menu links. + * + * @return + * An array with LinkitProfile objects. + */ +function linkit_profile_menu_load_all() { + $profiles = linkit_profile_load_all(); + foreach ($profiles as &$profile) { + if ($profile->profile_type != LINKIT_PROFILE_TYPE_MENU) { + $profile = FALSE; + } + } + return array_filter($profiles); +} + +/** * Temporary saves the active profile. Active means that the user is working * with the profile in the dialog. * @@ -1124,6 +1145,8 @@ function linkit_get_profile_type($type) { return t('Editor'); case LINKIT_PROFILE_TYPE_FIELD: return t('Field'); + case LINKIT_PROFILE_TYPE_MENU: + return t('Menu'); default: return t('Can not find the profile type'); } @@ -1158,3 +1181,18 @@ function linkit_get_insert_plugin_processed_path(LinkitProfile $profile, $uri, $ return $path; } + +/** + * Implements hook_form_FORM_ID_alter() for "menu_edit_item". + */ +function linkit_form_menu_edit_item_alter(&$form, &$form_state) { + // If a 'menu' profile type exists, use it to add Linkit to menu links. + $profiles = linkit_profile_menu_load_all(); + foreach ($profiles as $profile) { + $form['link_path']['#linkit'] = array( + 'profile' => $profile->name, + 'button_text' => t($profile->data['button_text']), + ); + break; + } +} diff --git a/plugins/export_ui/linkit_profiles.inc b/plugins/export_ui/linkit_profiles.inc index e49bd07..c83f5e8 100644 --- a/plugins/export_ui/linkit_profiles.inc +++ b/plugins/export_ui/linkit_profiles.inc @@ -72,6 +72,11 @@ function linkit_profiles_form_validate(&$form, &$form_state) { && empty($form_state['values']['data']['insert_plugin']['plugin'])) { form_set_error('data][insert_plugin][plugin', t('You have to select a insert plugin for profiles used with fields.')); } + + // If the profile type is menu, then button text is required. + if ($form_state['values']['profile_type'] == LINKIT_PROFILE_TYPE_MENU && empty($form_state['values']['data']['button_text'])) { + form_set_error('data][profile_type][button_text', t('You have to provide the button text for profiles used with menu links.')); + } } /** @@ -90,6 +95,12 @@ function linkit_profiles_form_submit(&$form, &$form_state) { unset($form_state['values']['data']['text_formats']); } + // Force raw URL plugin and method for 'menu' profiles. + if ($form_state['values']['profile_type'] == LINKIT_PROFILE_TYPE_MENU) { + $form_state['values']['data']['insert_plugin']['plugin'] = 'raw_url'; + $form_state['values']['data']['insert_plugin']['url_method'] = LINKIT_URL_METHOD_RAW; + } + // Unset active tab state, we dont need to save this. unset($form_state['values']['data']['data__active_tab']); } @@ -118,6 +129,7 @@ function _linkit_build_profile_type_form_fields(&$form, LinkitProfile $profile) '#options' => array( LINKIT_PROFILE_TYPE_EDITOR => t('Editors'), LINKIT_PROFILE_TYPE_FIELD => t('Fields'), + LINKIT_PROFILE_TYPE_MENU => t('Menus'), ), LINKIT_PROFILE_TYPE_EDITOR => array( '#description' => t('This profile will be used with editors like CKeditor or TinyMCE.'), @@ -125,6 +137,9 @@ function _linkit_build_profile_type_form_fields(&$form, LinkitProfile $profile) LINKIT_PROFILE_TYPE_FIELD => array( '#description' => t('This profile will be used with fields that do not have any editors.'), ), + LINKIT_PROFILE_TYPE_MENU => array( + '#description' => t('This profile will be used with menu links.'), + ), '#default_value' => isset($profile->profile_type) ? $profile->profile_type : 'editor', '#required' => TRUE, '#parents' => array('profile_type'), @@ -151,6 +166,22 @@ function _linkit_build_profile_type_form_fields(&$form, LinkitProfile $profile) ), '#parents' => array('data', 'text_formats'), ); + + // Button text (for Menu type only). + $form['data']['profile_type']['button_text'] = array( + '#type' => 'textfield', + '#title' => t('Button text that activates Linkit modal'), + '#default_value' => isset($profile->data['button_text']) ? $profile->data['button_text'] : t('Search'), + '#states' => array( + 'visible' => array( + 'input[name="profile_type"]' => array('value' => LINKIT_PROFILE_TYPE_MENU), + ), + 'required' => array( + 'input[name="profile_type"]' => array('value' => LINKIT_PROFILE_TYPE_MENU), + ), + ), + '#parents' => array('data', 'button_text'), + ); } /** @@ -496,4 +527,4 @@ function linkit_isnumeric_validate($element, &$form_state, $form) { if (!empty($element['#value']) && !is_numeric($element['#value'])) { form_error($element, $element['#title'] . ' should only contains numbers.'); } -} \ No newline at end of file +}