metatag_handler_field_entity.inc in Metatag 7
Main handler for Metatag data.
File
views/metatag_handler_field_entity.inc
View source
<?php
function _metatag_entity_type_from_table($table) {
$base_tables =& drupal_static(__FUNCTION__);
if (empty($base_tables)) {
foreach (entity_get_info() as $entity_type => $info) {
$base_tables[$info['base table']] = $entity_type;
}
}
return isset($base_tables[$table]) ? $base_tables[$table] : NULL;
}
class metatag_handler_field_entity extends views_handler_field {
public $entity_type_alias;
public $entity_id_alias;
public function get_base_table() {
$base_table = $this->view->base_table;
if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
$relationships = $this->view->display_handler
->get_option('relationships');
if (!empty($relationships[$this->options['relationship']])) {
$options = $relationships[$this->options['relationship']];
$data = views_fetch_data($options['table']);
$base_table = $data[$options['field']]['relationship']['base'];
}
}
return $base_table;
}
public function query() {
parent::query();
$base_table = $this
->get_base_table();
$base_table_alias = isset($this->relationship) ? $this->relationship : $base_table;
$entity_type = _metatag_entity_type_from_table($base_table);
$entity_info = entity_get_info($entity_type);
$this->entity_id_alias = $this->query
->add_field($base_table_alias, $entity_info['entity keys']['id']);
$entity_type_alias = $this->table_alias . '_' . $entity_type . '_entity_type';
$this->entity_type_alias = $this->query
->add_field(NULL, "'{$entity_type}'", $entity_type_alias);
}
public function get_entity_type($values) {
return isset($values->{$this->entity_type_alias}) ? $values->{$this->entity_type_alias} : NULL;
}
public function get_entity($values) {
$entity_type = $this
->get_entity_type($values);
$entity_id = isset($values->{$this->entity_id_alias}) ? $values->{$this->entity_id_alias} : NULL;
if (!is_null($entity_id)) {
$entities = entity_load($entity_type, array(
$entity_id,
));
return reset($entities);
}
return NULL;
}
}