Skip to content

Instantly share code, notes, and snippets.

@salvamomo
Created April 16, 2015 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salvamomo/0c12a926f951edf93274 to your computer and use it in GitHub Desktop.
Save salvamomo/0c12a926f951edf93274 to your computer and use it in GitHub Desktop.
Reply Add Form
<?php
/**
* @file
* Plugin to display the reply add form for a given entity and field.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'single' => TRUE,
'title' => t('Reply - Add form'),
'description' => t('Form to add a new reply to an Entity'),
'category' => t('Reply'),
'defaults' => array(),
'render callback' => 'reply_add_form_content_type_render',
'edit form' => array(
'reply_add_form_select_entity_type' => t('Reply form - Entity Type'),
'reply_add_form_select_reply_field' => t('Reply form - Reply Field'),
),
'required context' => array(
new ctools_context_required(t('Entity being viewed'), 'any'),
),
);
/**
* Output function for the 'reply_add_form' content type.
*/
function reply_add_form_content_type_render($subtype, $conf, $panel_args, $context) {
if (!$context[0]->data) {
return;
}
$entity = $context[0]->data;
$reply_field_instance = field_info_instance($conf['reply_entity_type'], $conf['reply_field'], $conf['reply_entity_type']);
$block = new stdClass();
$block->title = '';
$form_options = array(
'entity_id' => $entity->identifier(),
'instance_id' => $reply_field_instance['id'],
);
$output = drupal_get_form('reply_form', (object) $form_options);
$block->content = array(
'#markup' => render($output),
);
return $block;
}
/**
* Returns the administrative title for reply_add_form.
*/
function reply_add_form_content_type_admin_title($subtype, $conf) {
return t('Reply - Add form');
}
/**
* Returns an edit form for custom type settings.
*/
function reply_add_form_select_entity_type($form, &$form_state) {
$entities = entity_get_info();
$options = array();
// Get all existing entities.
foreach ($entities as $entity_type => $entity) {
$options[$entity_type] = $entity['label'];
}
$form['config']['reply_entity_type'] = array(
'#type' => 'select',
'#title' => t('Entity Type'),
'#options' => $options,
'#default_value' => isset($form_state['conf']['reply_entity_type']) ? $form_state['conf']['reply_entity_type'] : NULL,
);
return $form;
}
/**
* Returns an edit form for custom type settings.
*/
function reply_add_form_select_reply_field($form, &$form_state) {
$options = array();
// Get entity type chosen in previous step.
$entity_type = $form_state['conf']['reply_entity_type'];
// Get all the field instances for the given entity type, and add the 'reply'
// ones as options.
$field_map = field_info_field_map();
$reply_fields = array_filter($field_map, '_reply_add_form_filter_reply_fields');
foreach ($reply_fields as $field_name => $fields_info) {
if (!empty($fields_info['bundles'][$entity_type])) {
$options[$field_name] = $field_name;
}
}
$form['config']['reply_field'] = array(
'#type' => 'select',
'#title' => t('Reply field'),
'#options' => $options,
'#default_value' => isset($form_state['conf']['reply_field']) ? $form_state['conf']['reply_field'] : NULL,
);
return $form;
}
/**
* Submit handler for the custom type settings form.
*/
function reply_add_form_select_entity_type_submit($form, &$form_state) {
$form_state['conf'] = array_merge($form_state['conf'], array_filter($form_state['values']));
}
/**
* Submit handler for the custom type settings form.
*/
function reply_add_form_select_reply_field_submit($form, &$form_state) {
reply_add_form_select_entity_type_submit($form, $form_state);
}
/**
* Returns whether a field is of 'reply' type.
*
* @param array $field
* An array with field data, as returned by field_info_field_map().
*
* @return bool
* TRUE, if the field is a 'reply' type field, FALSE otherwise.
*/
function _reply_add_form_filter_reply_fields($field) {
return ($field['type'] == 'reply');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment