Commit 17c0b8f8 authored by fago's avatar fago

further polish and fix the metadata generated from schema information

parent 59e0f064
......@@ -53,6 +53,14 @@
* - 'rules controller class': An optional controller class for providing Rules
* integration. The given class has to inherit from the default class being
* EntityDefaultRulesController.
* - 'metadata controller class': A controller class for providing Entity meta-
* data module integration, i.e. entity property info. By default some meta-
* data is generated from your hook_schema() information and *read access* is
* granted to that properties. From that the Entity metadata module also
* generates token integration for you, once activated.
* Override the controller class to adapt the defaults and to improve and
* complete the generated metadata. Defaults to the
* EntityDefaultMetadataController class.
*
* @see hook_entity_info()
*/
......
......@@ -7,7 +7,7 @@
*/
/**
* Default controller for generating Rules integration.
* Default controller for generating some basic metadata.
*/
class EntityDefaultMetadataController {
......@@ -19,13 +19,54 @@ class EntityDefaultMetadataController {
}
public function entityPropertyInfo() {
$entity_label = drupal_strtolower($this->info['label']);
// Provide defaults based on the schema.
$info['properties'] = entity_metadata_convert_schema($this->info['base table']);
foreach ($info['properties'] as $name => &$property) {
// Try to come up with better default labels.
$property['label'] = t('@entity @property', array('@entity' => $this->info['label'], '@property' => $name));
// Add a description.
$property['description'] = t('@entity "@property" property.', array('@entity' => drupal_ucfirst($entity_label), '@property' => $name));
}
// Set better metadata for known entity keys.
$id_key = $this->info['entity keys']['id'];
if (!empty($this->info['entity keys']['name']) && $key = $this->info['entity keys']['name']) {
$info['properties'][$key]['type'] = 'token';
$info['properties'][$key]['label'] = t('Machine-readable name');
$info['properties'][$key]['description'] = t('The machine-readable name identifying this @entity.', array('@entity' => $entity_label));
$info['properties'][$id_key]['label'] = t('Internal, numeric @entity ID', array('@entity' => $entity_label));
$info['properties'][$id_key]['description'] = t('The ID used to identify this @entity internally.', array('@entity' => $entity_label));
}
else {
$info['properties'][$id_key]['label'] = t('@entity ID', array('@entity' => drupal_ucfirst($entity_label)));
$info['properties'][$id_key]['description'] = t('The unique ID of the @entity.', array('@entity' => $entity_label));
}
// Care for the bundle.
if (!empty($this->info['entity keys']['bundle']) && $key = $this->info['entity keys']['bundle']) {
$info['properties'][$key]['type'] = 'token';
$info['properties'][$key]['options list'] = array(get_class($this), 'bundleOptionsList');
}
// Care for the label.
if (!empty($this->info['entity keys']['label']) && $key = $this->info['entity keys']['label']) {
$info['properties'][$key]['label'] = t('Label');
$info['properties'][$key]['description'] = t('The human readable label.');
}
return array($this->type => $info);
}
/**
* A options list callback returning all bundles for an entity type.
*/
public static function bundleOptionsList($name, $info) {
if (!empty($info['parent']) && $type = $info['parent']) {
$entity_info = $info['parent']->entityInfo();
$options = array();
foreach ($entity_info['bundles'] as $name => $bundle_info) {
$options[$name] = $bundle_info['label'];
}
return $options;
}
}
}
/**
......
......@@ -473,9 +473,10 @@ function entity_metadata_convert_schema($table) {
if ($type = _entity_metadata_convert_schema_type($info['type'])) {
$properties[$name] = array(
'type' => $type,
'label' => $name,
'setter callback' => 'entity_metadata_verbatim_set',
'label' => drupal_ucfirst($name),
'query callback' => 'entity_metadata_table_query',
// As we cannot know about any setter access, leave out the setter
// callback. For getting usually no further access callback is needed.
);
if ($type == 'serial') {
$properties[$name]['validation callback'] = 'entity_metadata_validate_integer_positive';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment