From 2bab89d53ed4dd2aa91499ac4545ec27bb58a2e2 Mon Sep 17 00:00:00 2001
From: Gordon Heydon <gordon@heydon.com.au>
Date: Thu, 14 Sep 2017 17:37:48 +1000
Subject: [PATCH] Issue #1534722 by broeker, skwashd, gordon: Make custom bean
 forms compatible with Display Suite

---
 bean.ds_fields_info.inc | 44 +++++++++++++++++++++++++++++++
 bean.module             | 69 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 113 insertions(+)
 create mode 100644 bean.ds_fields_info.inc

diff --git a/bean.ds_fields_info.inc b/bean.ds_fields_info.inc
new file mode 100644
index 0000000..97e7193
--- /dev/null
+++ b/bean.ds_fields_info.inc
@@ -0,0 +1,44 @@
+<?php
+/**
+ * @file
+ */
+
+/**
+ * Implements hook_ds_fields_info().
+ */
+function bean_ds_fields_info($entity_type) {
+  $fields = [];
+
+  if ($entity_type == 'bean') {
+    $fields[$entity_type]['label'] = [
+      'title' => t('Label'),
+      'field_type' => DS_FIELD_TYPE_FUNCTION,
+      'function' => 'bean_render_field',
+      'properties' => array(
+        'entity_render_key' => 'label',
+        'settings' => array(
+          'link' => array('type' => 'select', 'options' => array('no', 'yes')),
+          'wrapper' => array('type' => 'textfield', 'description' => t('Eg: h1, h2, p')),
+          'class' => array('type' => 'textfield', 'description' => t('Put a class on the wrapper. Eg: block-title')),
+        ),
+        'default' => array('wrapper' => 'h2', 'link' => 0, 'class' => ''),
+      )
+    ];
+    $fields[$entity_type]['title'] = [
+      'title' => t('Title'),
+      'field_type' => DS_FIELD_TYPE_FUNCTION,
+      'function' => 'bean_render_field',
+      'properties' => array(
+        'entity_render_key' => 'title',
+        'settings' => array(
+          'link' => array('type' => 'select', 'options' => array('no', 'yes')),
+          'wrapper' => array('type' => 'textfield', 'description' => t('Eg: h1, h2, p')),
+          'class' => array('type' => 'textfield', 'description' => t('Put a class on the wrapper. Eg: block-title')),
+        ),
+        'default' => array('wrapper' => 'h2', 'link' => 0, 'class' => ''),
+      ),
+    ];
+  }
+
+  return $fields;
+}
\ No newline at end of file
diff --git a/bean.module b/bean.module
index 4c05d18..1b1f6d0 100644
--- a/bean.module
+++ b/bean.module
@@ -1199,3 +1199,72 @@ function bean_ctools_plugin_directory($module, $plugin) {
     return 'plugins/' . $plugin;
   }
 }
+
+/**
+ * Implements hook_ds_field_format_summary().
+ */
+function bean_ds_field_format_summary($field) {
+  return ds_ds_field_format_summary($field);
+}
+
+/**
+ * Implements hook_ds_field_settings_form().
+ */
+function bean_ds_field_settings_form($field) {
+  return ds_ds_field_settings_form($field);
+}
+/**
+ * Render a field.
+ */
+function bean_render_field($field) {
+  $title_field = FALSE;
+
+  $output = '';
+  $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : array();
+  $settings += $field['properties']['default'];
+
+  // Basic string.
+  if (isset($settings['link text'])) {
+    $output = t($settings['link text']);
+  }
+  elseif (isset($field['properties']['entity_render_key']) && isset($field['entity']->{$field['properties']['entity_render_key']})) {
+    $title_field = $field['properties']['entity_render_key'] == 'title' && $field['entity_type'] == 'bean';
+    $output = $field['entity']->{$field['properties']['entity_render_key']};
+  }
+
+  if (empty($output)) {
+    return;
+  }
+
+  // Link.
+  if ($settings['link']) {
+    if (isset($field['entity']->uri)) {
+      $uri_info = $field['entity']->uri;
+    }
+    else {
+      $uri_info = entity_uri($field['entity_type'], $field['entity']);
+    }
+    if (isset($settings['link class'])) {
+      $uri_info['options']['attributes']['class'][] = $settings['link class'];
+    }
+    $output = l($output, $uri_info['path'], $uri_info['options']);
+    if ($title_field) {
+      $output = ds_edit_support('title', $output, $field);
+    }
+  }
+  else {
+    $output = check_plain($output);
+    if ($title_field) {
+      $output = ds_edit_support('title', $output, $field);
+    }
+  }
+
+  // Wrapper and class.
+  if (!empty($settings['wrapper'])) {
+    $wrapper = check_plain($settings['wrapper']);
+    $class = (!empty($settings['class'])) ? ' class="' . check_plain($settings['class']) . '"' : '';
+    $output = '<' . $wrapper . $class . '>' . $output . '</' . $wrapper . '>';
+  }
+
+  return $output;
+}
\ No newline at end of file
-- 
2.13.2

