1

I want to make my entity fieldable and manageable from admin area with manage display and manage field admin ui. I have read few tutorials, but I still not understand how to do it? This question is related to my other question. Here is my entity code.

function statuses_entity_info() {
  $backupControl = module_exists('entity') ? 'EntityAPIController' : 'DrupalDefaultEntityController';
  return array(
    'status' => array(
      'label' => t('Status'),
      'plural label' => t('Statuses'),
      'description' => t('A status update.'),
      'entity class' => 'StatusEntity',
      'controller class' => module_exists('statuses') ? 'StatusesEntityController' : $backupControl,
      'base table' => 'statuses',
      'load hook' => 'statuses_load',
      'uri callback' => 'statuses_uri',
      'module' => 'statuses',
      'entity keys' => array(
        'id' => 'sid',
        'label' => 'message',
      ),
       'bundles' => array(
        'status' => array(
          'label' => t('Status'),
        ),
      ),
      'view modes' => array(
        'full' => array(
          'label' => t('Status message'),
          'custom settings' => FALSE,
        ),
      ),
      'entity cache' => FALSE,
    ),
  );
}

function statuses_entity_property_info()  {
  $info = array();

  $info['status']['properties']['sid'] = array(
    'label' => t('Status ID'),
    'description' => t('The ID of the status'),
    'type' => 'integer',
    'schema field' => 'sid',
  );

  $info['status']['properties']['sender'] = array(
    'label' => t('Status sender'),
    'description' => t('Sender uid of the status message'),
    'type' => 'integer',
    'schema field' => 'sender',
  );
  $info['status']['properties']['recipient'] = array(
    'label' => t('Status recipient'),
    'description' => t('Recipient uid of the status message'),
    'type' => 'integer',
    'schema field' => 'recipient',
  );
  $info['status']['properties']['type'] = array(
    'label' => t('Type'),
    'description' => t('Type of status message'),
    'type' => 'text',
    'schema field' => 'type',
  );
  $info['status']['properties']['created'] = array(
    'label' => t('Created'),
    'description' => t('UNIX Timestamp of creation date'),
    'type' => 'integer',
    'schema field' => 'created',
  );
  $info['status']['properties']['message'] = array(
    'label' => t('Message'),
    'description' => t('Status message'),
    'type' => 'text',
    'schema field' => 'message',
  );
  return $info;
}

function statuses_uri($status)  {
  return array(
    'path' => 'statuses/' . $status->sid,
  );
}


class StatusesEntityController extends EntityAPIController  {
//TODO
}


if (class_exists('Entity')) {
  class StatusEntity extends Entity {
    public function __construct(array $values = array(), $entityType = NULL)  {
      parent::__construct($values, 'status');
    }

    public function buildContent($view_mode = 'full', $langcode = NULL) {
      $content['sender'] = array(
      '#markup' => 'Sender Markup TEST',
      );
      $content['recipient'] = array(
        '#markup' => 'Recipient Markup TEST',
      );
      $content['created'] = array(
        '#markup' => 'Created Markup TEST',
      );
      $content['message'] = array(
        '#markup' => 'Message Markup TEST',
      );
      $content['sid'] = array(
        '#markup' => 'sid Markup TEST',
      );
      $content['type'] = array(
        '#markup' => 'type Markup TEST',
      );
      return entity_get_controller($this->entityType)->buildContent($this, $view_mode, $langcode, $content);


    }
  }
}

1 Answer 1

2

Just add the fieldable key and set it to true:

return array(
  'status' => array(
    'label' => t('Status'),
    'plural label' => t('Statuses'),
    'fieldable' => TRUE,
    ...
7
  • 1
    Will it also add "manage display" and "manage field" links in admin area so that I can add fields to it?
    – Umair
    Commented Jan 4, 2017 at 7:06
  • 1
    What happened when you tried?
    – Clive
    Commented Jan 4, 2017 at 8:37
  • 1
    I haven't tried it yet because I guess I have to uninstall and re-install the module after making changes. That is why I asked.
    – Umair
    Commented Jan 4, 2017 at 15:06
  • 1
    Nothing happened
    – Umair
    Commented Jan 4, 2017 at 17:57
  • 1
    Yes you might need to reinstall the module. For the field UI there are a few extra steps, see the docs for info: drupal.org/docs/7/api/field-api/…
    – Clive
    Commented Jan 4, 2017 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.