diff --git a/fieldable_panels_panes.install b/fieldable_panels_panes.install index 3c54648..a58674d 100644 --- a/fieldable_panels_panes.install +++ b/fieldable_panels_panes.install @@ -41,11 +41,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', @@ -165,9 +160,57 @@ function fieldable_panels_panes_schema() { return $schema; } - /** - * Add UUID support. - */ +/** + * Implements hook_install(). + */ +function fieldable_panels_panes_install() { + $field = array( + 'field_name' => 'fieldable_panels_panes_path', + 'cadinality' => 1, + 'type' => 'text', + 'locked' => TRUE, + ); + + // During installation, the t() function is unavailable, so we use get_t() + // to store the name of the translation function. + $t = get_t(); + + field_create_field($field); + $field_instances = array( + 'field_fieldable_panels_panes_path' => array( + 'field_name' => 'fieldable_panels_panes_path', + '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, + ), + ), + ); + + //create the instance + $entity_info = entity_get_info('fieldable_panels_pane'); + foreach ($field_instances as $field_instance) { + foreach ($entity_info['bundles'] as $bundle => $info) { + $field_instance['entity_type'] = 'fieldable_panels_pane'; + $field_instance['bundle'] = $bundle; + field_create_instance($field_instance); + } + } +} + +/** + * Add UUID support. + */ function fieldable_panels_panes_update_7101(&$sandbox) { // Make sure to add UUID field @@ -233,3 +276,52 @@ function fieldable_panels_panes_update_7106() { )); } } + +/** + * Migrate the path data into a field instance. + */ +function fieldable_panels_panes_update_7107(&$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; + } + // miragte 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 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; + } +} + +/** + * Drop the old path off the entity. + */ +function fieldable_panels_panes_update_7108(&$sandbox) { + // remove the old path field. + db_drop_field('fieldable_panels_panes', 'path'); +} diff --git a/fieldable_panels_panes.module b/fieldable_panels_panes.module index d7a57b1..242d566 100644 --- a/fieldable_panels_panes.module +++ b/fieldable_panels_panes.module @@ -588,10 +588,15 @@ 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') { $entity = fieldable_panels_panes_load_entity($vars['pane']->subtype); + $items = field_get_items('fieldable_panels_pane', $entity, 'fieldable_panels_panes_path', $language->language); if ($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)); } $vars['classes_array'][] = 'pane-bundle-' . ctools_cleanstring($entity->bundle, array('lower case' => TRUE)); } @@ -812,14 +817,6 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) { '#id' => 'edit-link', ); - $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')), - '#dependency' => array('edit-link' => array(1)), - '#default_value' => $entity->path, - ); - $form['reusable'] = array( '#weight' => 10, ); @@ -890,6 +887,14 @@ 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['fieldable_panels_panes_path']['#states'] = array( + 'visible' => array( + ':input#edit-link' => array('checked' => TRUE), + ), + ); + if (!empty($form_state['add submit'])) { $form['actions'] = array( '#type' => 'actions', @@ -908,7 +913,29 @@ 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); + field_attach_form_validate('fieldable_panels_pane', $form_state['fieldable_panels_pane'], $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', $form_state['fieldable_panels_pane']); + } + if (isset($form_state['values']['fieldable_panels_panes_path'][$language][0]['value']) + && !empty($form_state['values']['fieldable_panels_panes_path'][$language][0]['value'])) { + // Check that the link exists + $source = valid_url($form_state['values']['fieldable_panels_panes_path'][$language][0]['value'], TRUE); + if (!$source) { + $normal_path = drupal_get_normal_path($form_state['values']['fieldable_panels_panes_path'][$language][0]['value']); + $source = drupal_valid_path($normal_path); + } + if (!$source) { + form_set_error('fieldable_panels_panes_path', t('The path / 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 / url, or unselect "Make title a link".')); + } + } } /** @@ -925,6 +952,7 @@ function fieldable_panels_panes_entity_edit_form_submit($form, &$form_state) { $entity->category = $form_state['values']['category']; $entity->admin_title = $form_state['values']['admin_title']; $entity->admin_description = $form_state['values']['admin_description']; + $entity->fieldable_panels_panes_path = $form_state['values']['fieldable_panels_panes_path']; $entity->revision = $form_state['values']['revision']; // Only set a log message if there was a new revision. This prevents