1

I am trying to create a custom entity with user manageable bundles with very little luck.

I have been using a couple of other modules that do this very successfully like field collection and the latest version of the invite module.

Very simply, there is an event entity which has multiple types. The user should be able to create and manage these multiple types - similar to how users can create and manage multiple field collections.

My entity info is as follows:

function enf_entity_info() {
  $return = array(
    'enf_event' => array(
      'label' => t('ENF Event'),
      'entity class' => 'ENFEvent',  // defined - simple
      'controller class' => 'ENFEventController', // defined - simple
      'base table' => 'enf_event', // defined
      'fieldable' => TRUE,
      'entity keys' => array(
        'id' => 'event_id',
        'bundle' => 'event_type',
      ),
      'bundle keys' => array(
        'bundle' => 'event_type',
      ),
      'view modes' => array(      // I don't expect to need these
        'full' => array(
          'label' => t('Default'),
          'custom settings' => FALSE,
        ),
      ),
      'bundles' => array(),
      'label callback' => 'entity_class_label',
      'uri callback' => 'entity_class_uri',
      'module' => 'enf',
      'access callback' => 'enf_event_access',  // simply returns TRUE
      'metadata controller class' => 'EntityDefaultMetadataController',
    ),
  );

  $return['enf_event_type'] = array(
    'label' => t('ENF Event Type'),
    'entity class' => 'ENFEventType', // defined - simple
    'controller class' => 'ENFEventTypeController', // defined - simple
    'base table' => 'enf_event_type', // defined
    'fieldable' => FALSE,
    'bundle of' => 'enf_event',
    'exportable' => TRUE,
    'entity keys' => array(
      'id' => 'type_id',
      'name' => 'event_type',
      'label' => 'label',
    ),
    'module' => 'enf',
    // Enable the entity API's admin UI.
    'admin ui' => array(
      'path' => 'admin/structure/enf-event-types',
      'file' => 'includes/enf.admin.inc', // includes the form functions
      //'controller class' => 'ENFEventTypeUIController',
    ),
    'access callback' => 'enf_event_type_access',
  );
  return $return;
}

the bundles are then added in in the alter

/**
 * Implements hook_entity_info_alter().
 */
function enf_entity_info_alter(&$entity_info) {
  foreach (enf_get_types() as $type => $info) {
    $entity_info['enf_event']['bundles'][$type] = array(
      'label' => $info->label,
      'admin' => array(
        'path' => 'admin/structure/enf-event-types/manage/%event_type',
        'real path' => 'admin/structure/enf-event-types/manage/' . $type,
        'bundle argument' => 5,
      ),
    );
  }
}

The admin page seems to work - however, there are manage fields and manage display menu options on the page itself. These go to a

/admin/structure/enf-event-types/manage/%25event_type/fields

and /admin/structure/enf-event-types/manage/%25event_type/display

and there are notifications displayed and saving and such errors out.

It is possible to add types - the form is displayed and creates a new type on submit.

However, when clicking through the fields or display links, a 404 comes back

/admin/structure/enf-event-types/manage/test/fields
/admin/structure/enf-event-types/manage/test/display

It seems like it should really be very simple to do this but something is missing.

Any assistance greatly appreciated...

1

A little bit late, however, hope this will be of use.

When defining an admin path for each bundle, path provides the path that should be used in hook_menu for the Field UI´s pages. For resolving the '%event_type' placeholder in admin paths, you need to provide a loader function that, in your case, should be 'event_type_load' and it must return a single type object.

/**
 * Event type loader function.
 *
 * param $type string
 *  The name of the event type as it appears in the admin Path placeholder 
 *  of a bundle. By convention, all Drupal paths use dashes instead of underscores.
 * 
 * return $type stdClass object
 * 
 */
function event_type_load($type) {
        $types = enf_get_types();
        // We need to replace dashes that may appear in $type with underscores.
        // By convention, bundle names needs to use underscores, not dashes.
        $type = str_replace('-', '_', $type);
        return isset($types[$type]) ? $types[$type] : FALSE;
}

You may need to modify as well the line that reads,

'real path' => 'admin/structure/enf-event-types/manage/' . $type,

and replace it with the following one:

'real path' => 'admin/structure/enf-event-types/manage/' . str_replace('_', '-', $type),

NOTE: to be consistent with the name of the module that defines your custom entity, you may want to rename the admin path placeholder '%event_type' to be '%enf_event_type', and thus the loader function would be 'enf_event_type_load'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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