diff --git a/flag.module b/flag.module index 0931992..4a1eee1 100644 --- a/flag.module +++ b/flag.module @@ -2286,3 +2286,12 @@ function flag_set_sid($uid = NULL, $create = TRUE) { function flag_get_sid($uid = NULL, $create = FALSE) { return flag_set_sid($uid, $create); } + +/** + * Implements hook_ctools_plugin_directory(). + */ +function flag_ctools_plugin_directory($module, $plugin) { + if ($module == 'ctools' && !empty($plugin)) { + return "plugins/$plugin"; + } +} diff --git a/plugins/content_types/flag_link/flag_link.inc b/plugins/content_types/flag_link/flag_link.inc new file mode 100644 index 0000000..33de132 --- /dev/null +++ b/plugins/content_types/flag_link/flag_link.inc @@ -0,0 +1,115 @@ + t('Flag link'), + 'description' => t('Add a flag link of a certain flag type for entities.'), + 'defaults' => array('flag_name' => ''), + 'content type' => 'flag_flag_link_content_type_info', +); + +/** + * Get the entity content type info. + */ +function flag_flag_link_content_type_info($entity_type) { + $types = flag_flag_link_content_type_content_types(); + if (isset($types[$entity_type])) { + return $types[$entity_type]; + } +} + +/** + * Implements hook_PLUGIN_content_type_content_types(). + */ +function flag_flag_link_content_type_content_types() { + $types = array(); + $entities = entity_get_info(); + + foreach ($entities as $entity_type => $info) { + if (entity_type_supports($entity_type, 'view')) { + $types[$entity_type] = array( + 'title' => t('Flag for @entity_type', array('@entity_type' => $info['label'])), + 'category' => t('Entity'), + 'required context' => new ctools_context_required(t('Entity'), $entity_type), + ); + } + } + + return $types; +} + +/** + * Render callback. + */ +function flag_flag_link_content_type_render($subtype, $conf, $args, $context) { + $flag = flag_get_flag($conf['flag_name']); + if (!$flag) { + return; + } + + if (empty($context->data)) { + return; + } + + // Get the ID of the entity. + list($id,,) = entity_extract_ids($flag->entity_type, $context->data); + $link = flag_create_link($flag->name, $id); + + if (!$link) { + return; + } + + $block = new stdClass(); + $block->module = 'flag'; + $block->title = t('Flag link'); + $block->delta = $flag->name; + + $block->content = $link; + return $block; +} + +/** + * Form callback. + */ +function flag_flag_link_content_type_edit_form($form, &$form_state) { + $conf = $form_state['conf']; + $entity_type = $form_state['subtype_name']; + $options = array(); + + foreach (flag_get_flags($entity_type) as $flag) { + $options[$flag->name] = $flag->title; + } + + $form['flag_name'] = array( + '#type' => 'select', + '#title' => t('Flag name'), + '#default_value' => $conf['flag_name'], + '#options' => $options, + '#description' => t('Select a flag.'), + '#required' => TRUE, + '#disabled' => !$options, + ); + + return $form; +} + +/** + * Form submit. + */ +function flag_flag_link_content_type_edit_form_submit($form, &$form_state) { + // Copy everything from our defaults. + foreach (array_keys($form_state['plugin']['defaults']) as $key) { + $form_state['conf'][$key] = $form_state['values'][$key]; + } +} + +/** + * Returns the administrative title for a flag link. + */ +function flag_flag_link_content_type_admin_title($subtype, $conf) { + if ($flag = flag_get_flag($conf['flag_name'])) { + return t('Flag link of @flag', array('@flag' => $flag->title)); + } +}