From aaea79bb247ec9f66d5be330608fa875698216df Mon Sep 17 00:00:00 2001
From: jucallme <jucallme@gmail.com>
Date: Thu, 15 Nov 2012 20:06:45 -0300
Subject: [PATCH] add entity translation support

---
 fieldable_panels_panes.info                        |  1 +
 fieldable_panels_panes.install                     | 28 +++++++++++++
 fieldable_panels_panes.module                      | 47 ++++++++++++++++++++--
 .../translation.handler.fieldable_panels_panes.inc | 30 ++++++++++++++
 plugins/content_types/fieldable_panels_pane.inc    | 11 +++++
 5 files changed, 114 insertions(+), 3 deletions(-)
 create mode 100644 includes/translation.handler.fieldable_panels_panes.inc

diff --git a/fieldable_panels_panes.info b/fieldable_panels_panes.info
index 836d6b1..0587875 100644
--- a/fieldable_panels_panes.info
+++ b/fieldable_panels_panes.info
@@ -7,6 +7,7 @@ dependencies[] = "views"
 dependencies[] = "field_ui"
 
 files[] = includes/PanelsPaneController.class.php
+files[] = includes/translation.handler.fieldable_panels_panes.inc
 files[] = plugins/views/fieldable_panels_panes_handler_argument_bundle.inc
 files[] = plugins/views/fieldable_panels_panes_handler_field_bundle.inc
 files[] = plugins/views/fieldable_panels_panes_handler_field_delete_entity.inc
diff --git a/fieldable_panels_panes.install b/fieldable_panels_panes.install
index e5f8747..50396ce 100644
--- a/fieldable_panels_panes.install
+++ b/fieldable_panels_panes.install
@@ -64,6 +64,13 @@ function fieldable_panels_panes_schema() {
         'description' => 'The category it will appear in the Add Content dialog under.',
         'type' => 'text',
       ),
+      'language' => array(
+        'description' => 'The language of this entity.',
+        'type' => 'varchar',
+        'length' => 32,
+        'not null' => TRUE,
+        'default' => '',
+      ),
       'view_access' => array(
         'type' => 'text',
         'size' => 'big',
@@ -210,3 +217,24 @@ function fieldable_panels_panes_update_7105() {
   db_add_field('fieldable_panels_panes', 'link', array('type' => 'int', 'size' => 'tiny', 'description' => 'Whether or not this entity title will link to another page.'));
   db_add_field('fieldable_panels_panes', 'path', array('type' => 'varchar', 'length' => 255, 'description' => 'The path the title should link to.'));
 }
+
+/*
+ * Add language column.
+ */
+function fieldable_panels_panes_update_7107(&$sandbox) {
+  $table = 'fieldable_panels_panes';
+  $field = 'language';
+
+  if (!db_field_exists($table, $field)) {
+    $schema = drupal_get_schema($table, TRUE);
+    db_add_field($table, $field, $schema['fields'][$field]);
+
+    db_update($table)
+      ->fields(array($field => LANGUAGE_NONE))
+      ->execute();
+
+    return t("Added $table.language field.");
+  }
+
+  return t("$table.language field already exists, updated skipped.");
+}
diff --git a/fieldable_panels_panes.module b/fieldable_panels_panes.module
index ca14b2a..efb7ce6 100644
--- a/fieldable_panels_panes.module
+++ b/fieldable_panels_panes.module
@@ -50,7 +50,34 @@ function fieldable_panels_panes_entity_info() {
           'custom settings' => FALSE,
         ),
       ),
-
+      // Add title replacement support for translations.
+      'field replacement' => array(
+        'title' => array(
+          'field' => array(
+            'type' => 'text',
+            'cardinality' => 1,
+            'translatable' => TRUE,
+          ),
+          'instance' => array(
+            'label' => t('Title'),
+            'required' => TRUE,
+            'settings' => array(
+              'text_processing' => 0,
+            ),
+            'widget' => array(
+              'weight' => -5,
+            ),
+          ),
+        ),
+      ),
+      'translation' => array(
+        'entity_translation' => array(
+          'class' => 'EntityTranslationFieldablePanelsPanesHandler',
+          'base path' =>  'admin/structure/fieldable-panels-panes/view/%fieldable_panels_panes',
+          'edit path' =>  'admin/structure/fieldable-panels-panes/view/%fieldable_panels_panes/edit',
+          'path wildcard' => '%fieldable_panels_panes',
+        ),
+      ),
       // entity module callbacks
       'view callback' => 'entity_metadata_view_single',
       'creation callback' => 'fieldable_panels_panes_create',
@@ -760,6 +787,12 @@ function fieldable_panels_panes_get_base_context($entity = NULL) {
  */
 function fieldable_panels_panes_entity_edit_form($form, &$form_state) {
   $entity = $form_state['entity'];
+  // Map these properties for entity translations.
+  $form['#entity_type'] = array(
+    '#type' => 'hidden',
+    '#value' => $entity->bundle,
+  );
+  $form_state['fieldable_panels_pane'] = $form_state['entity'];
 
   // fixes a bug in wysiwyg
   // oh wysiwyg. We need a better way to address this.
@@ -858,8 +891,16 @@ function fieldable_panels_panes_entity_edit_form($form, &$form_state) {
     '#dependency' => array('edit-revision' => array(1)),
     '#default_value' => '',
   );
-
-  field_attach_form('fieldable_panels_pane', $entity, $form, $form_state);
+  // Provide support for translations
+  if (module_exists('entity_translation')
+      && entity_translation_enabled('fieldable_panels_pane')) {
+    $handler = entity_translation_get_handler('fieldable_panels_pane', $entity);
+    $langcode = $handler->getFormLanguage();
+  }
+  else {
+    $langcode = LANGUAGE_NONE;
+  }
+  field_attach_form('fieldable_panels_pane', $entity, $form, $form_state, $langcode);
 
   if (!empty($form_state['add submit'])) {
     $form['actions'] = array(
diff --git a/includes/translation.handler.fieldable_panels_panes.inc b/includes/translation.handler.fieldable_panels_panes.inc
new file mode 100644
index 0000000..ab29e39
--- /dev/null
+++ b/includes/translation.handler.fieldable_panels_panes.inc
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * @file
+ * Fieldable panels panes translation handler for the translation module.
+ */
+
+
+/**
+ * Bean translation handler.
+ *
+ * Overrides default behaviors for Fieldable panels panes properties.
+ */
+class EntityTranslationFieldablePanelsPanesHandler extends EntityTranslationDefaultHandler {
+
+  public function __construct($entity_type, $entity_info, $entity) {
+    parent::__construct('fieldable_panels_pane', $entity_info, $entity);
+  }
+
+  public function getAccess($op) {
+    return fieldable_panels_panes_access($op, $this->entity);
+  }
+
+  protected function getEntityId() {
+    if (isset($this->entity->fpid)) {
+      return $this->entity->fpid;
+    }
+    return parent::getEntityId();
+  }
+}
diff --git a/plugins/content_types/fieldable_panels_pane.inc b/plugins/content_types/fieldable_panels_pane.inc
index 3f25fa2..9f23a15 100644
--- a/plugins/content_types/fieldable_panels_pane.inc
+++ b/plugins/content_types/fieldable_panels_pane.inc
@@ -89,6 +89,17 @@ function fieldable_panels_panes_fieldable_panels_pane_content_type_render($subty
       );
     }
 
+    if (module_exists('entity_translation')
+      && entity_translation_enabled('fieldable_panels_pane')
+      && entity_translation_tab_access('fieldable_panels_pane', $entity)) {
+      $block->admin_links[] = array(
+        'title' => t('Translate pane'),
+        'alt' => t("Translate the values in this pane"),
+        'href' => "admin/structure/fieldable-panels-panes/view/$entity->fpid/translate",
+        'query' => drupal_get_destination(),
+      );
+    }
+
     return $block;
   }
 }
-- 
1.7.11.3

