diff --git a/fieldable_panels_panes.info.inc b/fieldable_panels_panes.info.inc index b9d080b..0a5efbb 100644 --- a/fieldable_panels_panes.info.inc +++ b/fieldable_panels_panes.info.inc @@ -79,18 +79,6 @@ function fieldable_panels_panes_entity_property_info() { 'description' => t('Whether or not this entity title will link to another page.'), 'type' => 'boolean', ) + $default_properties; - $properties['path'] = array( - 'label' => t('Path'), - 'description' => t('The path the title should link to, uses alias if one available.'), - 'getter callback' => 'fieldable_panels_panes_metadata_fpp_get_properties', - 'type' => 'uri', - ) + $default_properties; - $properties['path_raw'] = array( - 'label' => t('Path raw'), - 'description' => t('The path the title should link to, excluding the alias.'), - 'schema field' => 'path', - 'type' => 'uri', - ) + $default_properties; $properties['reusable'] = array( 'label' => t('Reusable?'), 'description' => t('Whether or not this entity will appear in the Add Content dialog.'), diff --git a/fieldable_panels_panes.install b/fieldable_panels_panes.install index 9bfc67d..fd39c0d 100644 --- a/fieldable_panels_panes.install +++ b/fieldable_panels_panes.install @@ -17,8 +17,25 @@ function fieldable_panels_panes_install() { $item->title = t('Panels Pane'); ctools_export_crud_save('fieldable_panels_pane_type', $item); } + + // Create the path field instance. + _fieldable_panels_panes_create_field_path(); } +/** + * Loops over the instances and add the path field instance. + */ +function _fieldable_panels_panes_create_field_path() { + if (!function_exists('fieldable_panels_panes_add_path_instance')) { + // We make sure that the function is loaded. + module_load_include('module', 'fieldable_panels_panes'); + } + // Create the instances. + $entity_info = entity_get_info('fieldable_panels_pane'); + foreach ($entity_info['bundles'] as $bundle => $info) { + fieldable_panels_panes_add_path_instance($bundle); + } +} /** * Implements hook_uninstall(). @@ -70,11 +87,6 @@ function fieldable_panels_panes_schema() { 'type' => 'int', 'size' => 'tiny', ), - 'path' => array( - 'description' => 'The path the title should link to.', - 'type' => 'varchar', - 'length' => 255, - ), 'reusable' => array( 'description' => 'Whether or not this entity will appear in the Add Content dialog.', 'type' => 'int', @@ -413,3 +425,59 @@ function fieldable_panels_panes_update_7109() { 'default' => '', )); } + +/** + * Create the path FAPI field. + */ +function fieldable_panels_panes_update_7110() { + _fieldable_panels_panes_create_field_path(); +} + +/** + * Migrate the path to a FAPI field. + */ +function fieldable_panels_panes_update_7111(&$sandbox) { + if (!isset($sandbox['progress'])) { + $sandbox['progress'] = 0; + $sandbox['current_id'] = 0; + $sandbox['max'] = db_query('SELECT COUNT(DISTINCT fpid) FROM {fieldable_panels_panes}')->fetchField() - + 1; + } + // Migrate all path data into the field. + $query = db_select('fieldable_panels_panes', 'f'); + + $query->condition('f.fpid', $sandbox['current_id'], '>') + ->condition('f.path', '', '<>') + ->fields('f', array('fpid', 'path')) + ->orderBy('f.fpid', 'ASC') + ->range(0, 50); + + $results = $query->execute(); + + if ($results->rowCount() > 0) { + foreach ($results as $result) { + $entity = fieldable_panels_panes_load($result->fpid); + // The default is to enable this field but NOT to make it translatable + // as not every one will use that. So we can only set the default value + // of LANGUAGE_NONE (und). + if (isset($entity->bundle)) { + $entity->fieldable_panels_panes_path[LANGUAGE_NONE][0]['value'] = $result->path; + fieldable_panels_panes_save($entity); + } + $sandbox['current_id'] = $entity->fpid; + $sandbox['progress']++; + } + $sandbox['#finished'] = empty($sandbox['max']) ? TRUE : + ($sandbox['progress'] / $sandbox['max']); + } + else { + $sandbox['#finished'] = TRUE; + } +} + +/** + * Remove the path property from the entity. + */ +function fieldable_panels_panes_update_7112() { + db_drop_field('fieldable_panels_panes', 'path'); +} diff --git a/fieldable_panels_panes.module b/fieldable_panels_panes.module index e96881a..19e0fbf 100644 --- a/fieldable_panels_panes.module +++ b/fieldable_panels_panes.module @@ -757,11 +757,17 @@ function template_preprocess_fieldable_panels_pane(&$vars) { * Add a class for our bundle to the normal panels pane theme. */ function fieldable_panels_panes_preprocess_panels_pane(&$vars) { + global $language; if ($vars['pane']->type == 'fieldable_panels_pane') { if (!empty($vars['content']['#fieldable_panels_pane']) && is_object($vars['content']['#fieldable_panels_pane'])) { $entity = $vars['content']['#fieldable_panels_pane']; + $items = field_get_items('fieldable_panels_pane', $entity, 'fieldable_panels_panes_path', $language->language); if (!empty($entity->link) && !empty($vars['title'])) { - $vars['title'] = l($vars['title'], $entity->path); + $vars['title'] = l($vars['title'], $items[0]['value']); + } + if ($entity->link && !empty($vars['content']['title_field'])) { + $vars['content']['title_field'][0]['#markup'] = l($vars['content']['title_field'][0]['#markup'], + $items[0]['value'], array('html' => TRUE)); } ctools_include('cleanstring'); $vars['classes_array'][] = 'pane-bundle-' . ctools_cleanstring($entity->bundle, array('lower case' => TRUE)); @@ -1002,18 +1008,6 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) { '#description' => t('Check here to make the title link to another page.'), ); - $form['link']['path'] = array( - '#type' => 'textfield', - '#title' => t('Path'), - '#description' => t('The path for this link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')), - '#states' => array( - 'visible' => array( - ':input[name="link"]' => array('checked' => TRUE), - ), - ), - '#default_value' => $entity->path, - ); - $form['additional_settings'] = array( '#type' => 'vertical_tabs', '#weight' => 99, @@ -1129,6 +1123,16 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) { } field_attach_form('fieldable_panels_pane', $entity, $form, $form_state, $language); + // Shift the fieldable_panels_panes_path into the link fold and make it + // dependent on link. + $form['link']['fieldable_panels_panes_path'] = $form['fieldable_panels_panes_path']; + unset($form['fieldable_panels_panes_path']); + $form['link']['fieldable_panels_panes_path']['#states'] = array( + 'visible' => array( + ':input[name="link"]' => array('checked' => TRUE), + ), + ); + if (!empty($form_state['add submit'])) { $form['actions'] = array( '#type' => 'actions', @@ -1147,7 +1151,37 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) { * Validate callback for the pane entity. */ function fieldable_panels_panes_entity_edit_form_validate($form, &$form_state) { - field_attach_form_validate('fieldable_panels_pane', $form_state['entity'], $form, $form_state); + $entity = $form_state['entity']; + + field_attach_form_validate('fieldable_panels_pane', $entity, $form, $form_state); + if ($form_state['values']['link']) { + $language = NULL; + if (function_exists('entity_language')) { + // entity_language() was added in Drupal 7.15. + $language = entity_language('fieldable_panels_pane', $entity); + if (empty($language)) { $language = LANGUAGE_NONE; } + } + + if (isset($form_state['values']['fieldable_panels_panes_path'][$language][0]['value']) && + !empty($form_state['values']['fieldable_panels_panes_path'][$language][0]['value']) + ) { + $raw_path = $form_state['values']['fieldable_panels_panes_path'][$language][0]['value']; + // Check that the link exists + $source = valid_url($raw_path, TRUE); + if (!$source) { + $normal_path = drupal_get_normal_path($raw_path); + $source = drupal_valid_path($normal_path); + } + if (!$source) { + form_set_error('fieldable_panels_panes_path', + t('The path or url does not exist.')); + } + } + else { + form_set_error('fieldable_panels_panes_path', + t('You have selected that the title links to a path. Please enter a valid path or url, or unselect "Make title a link".')); + } + } } /** @@ -1159,7 +1193,6 @@ function fieldable_panels_panes_entity_edit_form_submit($form, &$form_state) { // Copy hardcoded fields. $entity->title = $form_state['values']['title']; $entity->link = $form_state['values']['link']; - $entity->path = $form_state['values']['path']; $entity->language = $form_state['values']['language']; $entity->reusable = $form_state['values']['reusable']; $entity->category = $form_state['values']['category']; @@ -1182,6 +1215,58 @@ function fieldable_panels_panes_entity_edit_form_submit($form, &$form_state) { } /** + * Adds a fieldable_panels_panes_path field to a fieldable_panels_panes type. + * + * @param string $bundle + * A fieldable_panels_panes bundle name. + * + * @return array + */ +function fieldable_panels_panes_add_path_instance($bundle) { + // Add or remove the path field, as needed. + $field = field_info_field('fieldable_panels_panes_path'); + if (empty($field)) { + $field = array( + 'field_name' => 'fieldable_panels_panes_path', + 'cadinality' => 1, + 'type' => 'text', + 'locked' => TRUE, + ); + $field = field_create_field($field); + } + + $instance = field_info_instance('fieldable_panels_pane', 'fieldable_panels_panes_path', $bundle); + if (empty($instance)) { + // During installation, the t() function is unavailable, so we use get_t() + // to store the name of the translation function. + $t = get_t(); + + $instance = array( + 'field_name' => 'fieldable_panels_panes_path', + 'entity_type' => 'fieldable_panels_pane', + 'bundle' => $bundle, + 'label' => $t('Path'), + 'description' => $t('The path the title should link to.'), + 'required' => FALSE, + 'widget' => array( + 'type' => 'fieldable_panels_panes_path', + ), + 'display' => array( + 'default' => array( + 'label' => 'hidden', + 'type' => 'hidden', + ), + ), + 'settings' => array( + 'text_processing' => 0, + ), + ); + $instance = field_create_instance($instance); + } + return $instance; +} + +/** * Returns whether the current page is the preview view of the passed-in pane. * * @param object $pane diff --git a/includes/PanelsPaneController.class.php b/includes/PanelsPaneController.class.php index 6f3b5ad..7dd566c 100644 --- a/includes/PanelsPaneController.class.php +++ b/includes/PanelsPaneController.class.php @@ -335,7 +335,6 @@ class PanelsPaneController extends DrupalDefaultEntityController { 'bundle' => 'fieldable_panels_pane', 'title' => '', 'link' => '', - 'path' => '', 'reusable' => FALSE, 'admin_title' => '', 'admin_description' => '', diff --git a/includes/fieldable_panels_pane.migrate.inc b/includes/fieldable_panels_pane.migrate.inc index a7c9682..4787b49 100644 --- a/includes/fieldable_panels_pane.migrate.inc +++ b/includes/fieldable_panels_pane.migrate.inc @@ -68,7 +68,6 @@ class MigrateDestinationFieldablePanelsPanes extends MigrateDestinationEntity { $fields['bundle'] = t('The bundle of the entity.'); $fields['title'] = t('The title of the entity.'); $fields['link'] = t('Whether or not this entity title will link to another page.'); - $fields['path'] = t('The path the title should link to.'); $fields['reusable'] = t('Whether or not this entity will appear in the Add Content dialog.'); $fields['admin_title'] = t('The title it will appear in the Add Content dialog as.'); $fields['admin_description'] = t('The description it will appear in the Add Content dialog with.'); diff --git a/plugins/export_ui/fieldable_panels_pane.class.php b/plugins/export_ui/fieldable_panels_pane.class.php index 39ddad5..25a9e7e 100644 --- a/plugins/export_ui/fieldable_panels_pane.class.php +++ b/plugins/export_ui/fieldable_panels_pane.class.php @@ -74,6 +74,9 @@ class fieldable_panels_pane extends ctools_export_ui { function edit_save_form($form_state) { parent::edit_save_form($form_state); + $item = &$form_state['item']; + fieldable_panels_panes_add_path_instance($item->{'name'}); + entity_info_cache_clear(); menu_rebuild();