Skip to content

Instantly share code, notes, and snippets.

Created December 26, 2013 18:14
Show Gist options
  • Save anonymous/8136876 to your computer and use it in GitHub Desktop.
Save anonymous/8136876 to your computer and use it in GitHub Desktop.
Create custom field for display suite
/**
* Implements hook_ctools_plugin_api().
*/
function workshop_helper_ctools_plugin_api($module, $api) {
if ($module == "ds" && $api == "ds") {
return array("version" => "1");
}
return NULL;
}
/**
* Implements hook_ds_fields_info().
*/
function workshop_helper_ds_fields_info($entity_type) {
$fields = array();
$fields['node']['ds_prefix'] = array(
'title' => t('Prefix'),
'field_type' => DS_FIELD_TYPE_FUNCTION,
'function' => 'workshop_helper_ds_field__ds_prefix',
// settings & default: optional if you have a settings form for your field.
'properties' => array(
'settings' => array(
'wrapper' => array(
'type' => 'textfield',
'description' => t('Eg: div, h1, h2, p, span'),
),
'css_class' => array(
'type' => 'textfield',
'description' => t('Put a class on the wrapper. Eg: block-title'),
),
),
'default' => array(
'wrapper' => 'div',
'css_class' => '',
),
)
);
$fields['node']['ds_suffix'] = array(
'title' => t('Suffix'),
'field_type' => DS_FIELD_TYPE_FUNCTION,
'function' => 'workshop_helper_ds_field__ds_suffix',
// settings & default: optional if you have a settings form for your field.
'properties' => array(
'settings' => array(
'wrapper' => array(
'type' => 'textfield',
'description' => t('Eg: h1, h2, p, must same with Prefix settingX'),
)
),
'default' => array(
'wrapper' => 'div',
),
)
);
if (isset($fields[$entity_type])) {
return array($entity_type => $fields[$entity_type]);
}
return;
}
/**
* Implements hook_ds_field_settings_form().
*/
function workshop_helper_ds_field_settings_form($field) {
return ds_ds_field_settings_form($field);
}
/**
* Implements hook_ds_field_format_summary().
*/
function workshop_helper_ds_field_format_summary($field) {
return ds_ds_field_format_summary($field);
}
/**
* Implements hook_ds_field_settings_alter().
*/
function workshop_helper_ds_field_settings_alter(&$field_settings, $form, &$form_state) {
if (isset($field_settings['ds_prefix']) || isset($field_settings['ds_suffix'])) {
$field_settings['ds_prefix']['formatter_settings']['ft']['func'] = 'theme_ds_field_reset';
$field_settings['ds_suffix']['formatter_settings']['ft']['func'] = 'theme_ds_field_reset';
}
}
/**
* Render the ds_suffix field.
*/
function workshop_helper_ds_field__ds_suffix($field) {
$wrapper = isset($field['formatter_settings']['wrapper']) ? check_plain($field['formatter_settings']['wrapper']) : 'div';
$content = '</' . $wrapper . '>';
return $content;
}
/**
* Render the ds_prefix field.
*/
function workshop_helper_ds_field__ds_prefix($field) {
$wrapper = isset($field['formatter_settings']['wrapper']) ? check_plain($field['formatter_settings']['wrapper']) : 'div';
$css_class = empty($field['formatter_settings']['css_class']) ? '' : ' class="' . check_plain($field['formatter_settings']['css_class']) . '"';
$content = '<' . $wrapper . $css_class . '>';
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment