This sounds easy enough, but a bunch of things blow up in the Views wizard when you do this.

Comments

joachim’s picture

Status: Active » Needs work
StatusFileSize
new755 bytes

Patch with the declaration in case anyone else has time to take this further.

ericclaeren’s picture

I have tried to to use this, but it brings a lot of problems, all current flag handlers expect relations, and since there aren't, errors gallore.
There should be a mechanism in the handlers, to detect if the view is based on a relation or not. But i'm not that big of a views wizard :)

I managed to do something similar, because I needed a view based on flaggings with remote custom entities without a relation or a db table and it works. By creating handlers without relations and doing a entity_load in the get_value of the custom entity handler.

This is my approach.


/**
 * Implements hook_views_data_alter().
 */
function my_module_views_data_alter(&$items) {
  // Views data alteration if Flag module is enabled.
  if (module_exists('flag')) {
    // Add leesrijk item as field in flagging table.
    $entity_type = 'my_custom_entity';

    // Alter data for Flag module.
    if (entity_type_supports($entity_type, 'view')) {
      // Add base table for views support.
      if (!isset($items['flagging']['table']['base'])) {
        $items['flagging']['table']['base'] = array(
          'entity type' => 'flagging',
          'field' => 'flagging_id',
          'title' => t('Flagging'),
          'help' => t('Flaggings store the data that represents a user having flagged an entity'),
        );
      }

      $entity_info = entity_get_info();
      $info = isset($entity_info[$entity_type]) ? $entity_info[$entity_type] : array();

      // Add rendered entity field.
      $items['flagging']['entity_' . $entity_type] = array(
        'title' => t('Flagging: @entity-type', array('@entity-type' => $info['label'])),
        'help' => t('The @entity-type of the current relationship rendered using a view mode.', array('@entity-type' => $info['label'])),
        'field' => array(
          'real field' => 'entity object',
          'handler' => 'my_module_handler_field_flag_entity',
          'type' => $entity_type,
          'additional fields' => array(
            'entity_id' => array('table' => 'flagging', 'field' => 'entity_id'),
          ),
        ),
      );
      // Add entity ops field.
      $items['flagging']['ops_' . $entity_type] = array(
        'title' => t('Flag link: @entity-type', array('@entity-type' => $info['label'])),
        'help' => t('Display flag/unflag link for @entity-type.', array('@entity-type' => $info['label'])),
        'field' => array(
          'handler' => 'my_module_handler_field_flag_ops',
          'additional fields' => array(
            'entity_id' => array('table' => 'flagging', 'field' => 'entity_id'),
          ),
          'flag type' => $entity_type, // Add flag type as definition.
        ),
      );
    }
  }
}
damienmckenna’s picture

This would definitely be useful.

ivnish’s picture

Status: Needs work » Closed (outdated)

Drupal 7 is EOL. No more new features will be added. Issue will be closed, but patches are still here