I have following custom table

INSERT INTO `exposed` (`id`, `name`, `deadline`, `node_id`)
VALUES
(1, 'Danny', 1399477939, 1),
(2, 'Peter', 1399477957, 2);

and this table is exposed in views using hook_views_data(). read:http://www.sitepoint.com/exposing-tables-views-drupal-7/

Now I want to integrate this view table with EditableViews module. When I integrate I don't see (editable) lable in any of the above table fields.

As a summary, How do I use editableviews with my custom tables (non entity)

CommentFileSizeAuthor
#2 empty fields.PNG179.48 KBadhisugha
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim’s picture

Status: Active » Fixed

You need to either:

- declare your table, and then declare its properties to Entity API metadata
- declare your fields as editable to hook_views_data, and probably create custom handler classes for them

adhisugha’s picture

FileSize
179.48 KB

Thanks. I am trying option1.

I dont see the fields in "Metadata property" drop down while I try to add editable fields in my view.
empty fields
Following is the code.

/**
 * Implements hook_entity_info().
 */
function expose_entity_info() {
  return array(
    'exposed' => array(
      'label' => t('Exposed'),
      'base table' => 'exposed',
      'entity keys' => array(
        'id' => 'id',
         'bundle' => 'type',
      ),
      'bundle keys' => array(
        'bundle' => 'type',
      ),
      'bundles' => array(),
    ),
  );
}


/**
 * Implements hook_entity_property_info() on top of node module.
 *
 * @see entity_metadata_entity_property_info()
 */
function expose_entity_property_info() {
  $info = array();
  // Add meta-data about the basic exposed properties.
  $properties = &$info['exposed']['properties'];

  $properties['id'] = array(
    'label' => t("ID"),
    'type' => 'text',
    'description' => t("The unique ID of the node."),
    'setter callback' => 'entity_metadata_verbatim_set',
    'schema field' => 'id',
  );
  $properties['name'] = array(
    'label' => t("name"),
    'type' => 'text',
    'description' => t("The name."),
    'setter callback' => 'entity_metadata_verbatim_set',
    'schema field' => 'name',
  );
  $properties['deadline'] = array(
    'label' => t("deadline"),
    'type' => 'text',
    'description' => t("The deadline."),
    'setter callback' => 'entity_metadata_verbatim_set',
    'schema field' => 'deadline',
  );
}
adhisugha’s picture

Here is the basic working for integrating custom table with editable views. The entity type and bundle name is 'exposed'. I have not done anything for deleting entity. But this works fine for Save.

CREATE TABLE `exposed` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary key of the record.',
`name` varchar(255) DEFAULT NULL COMMENT 'A simple text field.',
`deadline` int(11) DEFAULT NULL COMMENT 'A date stored as an integer timestamp.',
`node_id` int(11) DEFAULT NULL COMMENT 'A Drupal node related to this record.',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='The base table for the information we want exposed to Views.'

exposed.module
============

/**
 * Implements hook_views_api().
 */
function expose_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'expose') . '/includes/views',
  );
}

/**
 * Implements hook_entity_info().
 */
function expose_entity_info() {
  return array(
    'exposed' => array(
      'label' => t('Exposed'),
      'base table' => 'exposed',
      'controller class' => 'EntityExposedController',
      'save callback' => 'entity_metadata_expose_save',
      'entity keys' => array(
        'id' => 'id',
      ),
      'bundles' => array(
          'exposed' => array(
	      'label' => t('Exposed'),
    	   ),
      ),
    ),
  );
}


/**
 * Implements hook_entity_property_info() on top of node module.
 *
 * @see entity_metadata_entity_property_info()
 */
function expose_entity_property_info() {
  $info = array();
  // Add meta-data about the basic node properties.
  $properties = &$info['exposed']['properties'];

  $properties['id'] = array(
    'label' => t("ID"),
    'type' => 'text',
    'description' => t("The unique ID of the node."),
    'schema field' => 'id',
  );
  $properties['name'] = array(
    'label' => t("name"),
    'type' => 'text',
    'description' => t("The name."),
    //'setter callback' => 'entity_metadata_verbatim_set',
    'schema field' => 'name',
  );
  $properties['deadline'] = array(
    'label' => t("deadline"),
    'type' => 'text',
    'description' => t("The deadline."),
    //'setter callback' => 'entity_metadata_verbatim_set',
    'schema field' => 'deadline',
  );

$properties['node_id'] = array(
    'label' => t("Node ID"),
    'type' => 'text',
    'description' => t("The Node ID."),
    //'setter callback' => 'entity_metadata_verbatim_set',
    'schema field' => 'node_id',
  );

// The schema properties we need to provide a setter for.
  // This among other things makes them visible to Migrate Extras' Entity API
  // destination plugin.
  $settable_properties = array(
    'id',
    'name',
    'deadline',
    'node_id',
  );
  foreach ($settable_properties as $property) {
    // The default setter suffices.
    $properties[$property]['setter callback'] = 'entity_property_verbatim_set';
  }
  return $info;
}

/**
 * Call back function for entity save
 * @param  $entity
 *  the entity object
 */
function entity_metadata_expose_save($entity) {

  return entity_get_controller('exposed')->save($entity);
}

/**
 * EntityExposedControllerInterface definition.
 *
 * We create an interface here because anyone could come along and
 * use hook_entity_info_alter() to change our controller class.
 * We want to let them know what methods our class needs in order
 * to function with the rest of the module, so here's a handy list.
 *
 * @see hook_entity_info_alter()
 */
interface EntityExposedControllerInterface
  extends DrupalEntityControllerInterface {

  /**
   * Create an entity.
   */
  public function create();

  /**
   * Save an entity.
   *
   * @param object $entity
   *   The entity to save.
   */
  public function save($entity);

  /**
   * Delete an entity.
   *
   * @param object $entity
   *   The entity to delete.
   */
  public function delete($entity);

}


/**
 * EntityExposedController extends DrupalDefaultEntityController.
 *
 * Our subclass of DrupalDefaultEntityController lets us add a few
 * important create, update, and delete methods.
 */
class EntityExposedController
  extends DrupalDefaultEntityController
  implements EntityExposedControllerInterface
  {

  /**
   * Create and return a new exposed entity.
   */
  public function create() {
    $entity = new stdClass();
    $entity->type = 'exposed';
    $entity->id = 0;
    $entity->bundle_type = 'exposed';
    $entity->name = '';
    return $entity;
  }

  /**
   * Saves the custom fields using drupal_write_record().
   */
  public function save($entity) {

    // Invoke hook_entity_presave().
    module_invoke_all('entity_presave', $entity, 'exposed');
    // The 'primary_keys' argument determines whether this will be an insert
    // or an update. So if the entity already has an ID, we'll specify
    // basic_id as the key.
    $primary_keys = $entity->id ? 'id' : array();
    // Write out the entity record.
    drupal_write_record('exposed', $entity, $primary_keys);
    // We're going to invoke either hook_entity_update() or
    // hook_entity_insert(), depending on whether or not this is a
    // new entity. We'll just store the name of hook_entity_insert()
    // and change it if we need to.
    $invocation = 'entity_insert';
    // Now we need to either insert or update the fields which are
    // attached to this entity. We use the same primary_keys logic
    // to determine whether to update or insert, and which hook we
    // need to invoke.
    if (empty($primary_keys)) {
      field_attach_insert('exposed', $entity);
    }
    else {
      field_attach_update('exposed', $entity);
      $invocation = 'entity_update';
    }
    // Invoke either hook_entity_update() or hook_entity_insert().
    module_invoke_all($invocation, $entity, 'exposed');
    return $entity;
  }

  /**
   * Delete a single entity.
   *
   * Really a convenience function for deleteMultiple().
   */
  public function delete($entity) {
    $this->deleteMultiple(array($entity));
  }

  /**
   * Delete one or more exposed entities.
   *
   * Deletion is unfortunately not supported in the base
   * DrupalDefaultEntityController class.
   *
   * @param array $entities
   *   An array of entity IDs or a single numeric ID.
   */
  public function deleteMultiple($entities) {
    $basic_ids = array();
    if (!empty($entities)) {
      $transaction = db_transaction();
      try {
        foreach ($entities as $entity) {
          // Invoke hook_entity_delete().
          module_invoke_all('entity_delete', $entity, 'entity');
          field_attach_delete('exposed', $entity);
          $basic_ids[] = $entity->id;
        }
        db_delete('exposed')
          ->condition('ids', $basic_ids, 'IN')
          ->execute();
      }
      catch (Exception $e) {
        $transaction->rollback();
        watchdog_exception('exposed', $e);
        throw $e;
      }
    }
  }
}

exposed.views.inc
==============

/**
 * Implements hook_views_data().
 */
function expose_views_data() {

  $data = array();

  // Putting the table into its own groups so that we can recognize in the UI
  // where it comes from
  $data['exposed']['table']['group'] = t('Exposed');

  // Making the 'exposed' table a base table so a View can created based on it
  $data['exposed']['table']['base'] = array(
      //new for ajax error
    'field' => 'id',
    'title' => t('Exposed'),
    'help' => t('Contains records we want exposed to Views.'),
  );

  $data['exposed']['table']['entity type'] = 'exposed';

  $data['exposed']['table']['join'] = array(
    'node' => array(
      'left_field' => 'nid',
      'field' => 'node_id',
    ),
  );

  // The ID field
  $data['exposed']['id'] = array(
    'title' => t('ID'),
    'help' => t('The record ID.'),
    'field' => array(
     // 'field' => 'id', // the real field. This could be left out since it is the same. - See more at: http://evolvingweb.ca/story/developing-views-part-i-describing-your-data-views#sthash.5w53R1S9.dpuf
      'handler' => 'views_handler_field_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
  );

  // The Name field
  $data['exposed']['name'] = array(
    'title' => t('Name'),
    'help' => t('The record name.'),
    'field' => array(
      //'field' => 'name', // the real field. This could be left out since it is the same. - See more at: http://evolvingweb.ca/story/developing-views-part-i-describing-your-data-views#sthash.5w53R1S9.dpuf
      'handler' => 'views_handler_field',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
  );

  // The Deadline field
  $data['exposed']['deadline'] = array(
    'title' => t('Deadline'),
    'help' => t('The record deadline.'),
    'field' => array(
      //'field' => 'deadline', // the real field. This could be left out since it is the same. - See more at: http://evolvingweb.ca/story/developing-views-part-i-describing-your-data-views#sthash.5w53R1S9.dpuf
      'handler' => 'views_handler_field_date',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort_date',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_date',
    ),
  );

  // The Node ID field
  $data['exposed']['node_id'] = array(
    'title' => t('Node ID'),
    'help' => t('The record node ID.'),
    'field' => array(
      //'field' => 'node_id', // the real field. This could be left out since it is the same. - See more at: http://evolvingweb.ca/story/developing-views-part-i-describing-your-data-views#sthash.5w53R1S9.dpuf
      'handler' => 'views_handler_field_node',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'relationship' => array(
      'base' => 'node',
      'field' => 'node_id',
      'handler' => 'views_handler_relationship',
      'label' => t('Node'),
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_node_nid',
      'numeric' => TRUE,
      'validate type' => 'nid',
    ),
  );

  return $data;
}

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kumarAbhi’s picture

kumarAbhi’s picture

kumarAbhi’s picture

hi all

after coding with above example i am not getting the enabled expose module in add new views and view page what is cause of it try to help me fix this issue