1

I have two entities: People and People Notes.

The People entity is in another database with the People Notes is in the local Drupal database.

Schema for People:

PeopleId
FirstName
LastName
DOB

Schema for People Notes:

NoteId
uid
PeopleId (entityReference)
Signed (Boolean)

From a post I found here, I've created a custom action called Signed to execute a PHP script with the code below -

global $user;
$entity_notes_array = array();
$entity_notes_array['uid'] = $user->uid;
$entity_notes_array['field_people_reference']['value'][0] = 1; // Using 1 to test the reference 
$entity_notes_array['field_signed']['value'][0] = 1;
$entity_notes = entity_create('people_notes', $entity_notes_array);
$entity_notes->save();

I have a view created that uses the People table as its base and then I bring in the People_Notes table through an entity reference. The view also has a VBO that executes the Signed script.

Two questions:

1 - How do I save the entity People Notes? The script process with no error messages and it successfully returns with "Action Signed Petition saved", but when I got to see what was created the admin page does not list the new entity.

2 - How do I reference the PeopleId from the view?

Additional Information on People Notes

 /**
  * Implements hook_entity_info().
  */

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

    $info['people_notes'] = array(
        'label' => t('People Notes'),
        'base table' => 'people_notes',
        'entity keys' => array(
            'id' => 'id',
            'label' => 'label',
        ),
        'label callback' => 'entity_class_label',
        'uri callback' => 'entity_class_uri',
        'entity class' => 'PeopleNotesEntity',
        'controller class' => 'PeopleNotesEntityController',
        'views controller class' => 'PeopleNotesViewsController',
        'admin ui' => array(
            'path' => 'admin/peoplenotes',
            'controller class' => 'PeopleNotesUIController',
            'menu wildcard' => '%notes',
            'file' => 'people_notes.admin.inc',
        ),
        'module' => 'people_notes',
        'access callback' => 'people_notes_access_callback',
        'fieldable' => TRUE,
        'bundles' => array(
            'people_notes' => array(
                'label' => t('People Notes'),
                'admin' => array(
                    'path' => 'admin/peoplenotes',
                    'access arguments' => array('admininster notes'),
                ),
            ),
        ),
        'view modes' => array(
            'full' => array(
                'label' => t('Full View'),
                'custom settings' => FALSE,
            ),
            'part' => array(
                'label' => t('Partial View'),
                'custom settings' => FALSE,
            ),
        ),
    );
    return $info;
}

function people_notes_menu() {
    $items = array();
    $items['peoplenotesdemo'] = array(
        'title' => 'People Notes Demo Page',
        'page callback' => 'people_notes_demo_page',
        'access callback' => TRUE,
        'menu' => 'navigation',
    );
    $items['notes/%notes'] = array(
        'title' => 'People Notes Entity Page',
        'title arguments' => array(1),
        'page callback' => 'people_notes_view_entity',
        'access arguments' => 'view notes',
        'page arguments' => array(1),
        'type' => MENU_CALLBACK,
    );
    return $items;  
}

function people_notes_load($id) {
    $people_notes = entity_load('people_notes', array($id));
    return array_pop($people_notes);
}

function people_notes_save($people_notes) {
  return $people_notes->save();
}

function people_notes_demo_page() {
    $people_notes = entity_load('people_notes', array(1));
    kpr($people_notes);

    return 'Hi Notes from Mark';
}

function people_notes_page_title($people_notes){
  return $people_notes->id;
}

/*function people_notes_view_entity($people_notes) {
    $people_notes_entity = entity_view('people_notes', array($people_notes->id => $people_notes));
    kpr($people_notes_entity);
    return 'Hello Mark';
}
*/
function people_notes_view_entity($people_notes, $view_mode = 'full') {
    //drupal_set_title('(' . $people_notes->id . ')');
    $people_notes_entity = entity_view('people_notes', array($people_notes->id => $people_notes));
    return $people_notes_entity;
}

function people_notes_permission() {
    return array(
        'administer notes' => array(
            'title' => t('Administer Notes'),
        ),
        'view notes' => array(
            'title' => t('View Notes on a person'),
        ),
    );
}

function people_notes_access_callback($op, $people_notes = NULL, $account = NULL) {
    if (($op == 'view' && user_access('view notes', $account)) || user_access('administer notes', $account)) {
        return TRUE;
    } else if ($user_access('administer notes', $account)) {
        return TRUE;
    }
    return FALSE;
}

/*
 * Controller classes are located in the includes direcotory
 *
 */
// ...
1

You will need to change the value to target_id. Entity references do not use value index

global $user;
$entity_notes_array = array();
$entity_notes_array['uid'] = $user->uid;
$entity_notes_array['field_people_reference'][LANGUAGE_NONE][0]['target_id'] = 1;
$entity_notes_array['field_signed'][LANGUAGE_NONE][0]['value'] = 1;
$entity_notes = entity_create('people_notes', $entity_notes_array);
$entity_notes->save();
  • Chad - thanks for the response. Neither worked however. I have added some additional information to make sure I'm setting up my entity correctly. Also, had a typo - entity_metadata_wrapper - you missed the 'a' in metadata. – Mark Parrish Nov 20 '14 at 19:27
  • Can you execute the second set of code in devel using execute php code and create an entity? – chadpeppers Nov 20 '14 at 19:45
  • Nope got the following error - EntityMetadataWrapperException: Entity property uid doesn't support writing. in EntityStructureWrapper->setProperty() (line 493 of /var/www/drupal7/sites/all/modules/contrib/entity/includes/entity.wrapper.inc). – Mark Parrish Nov 20 '14 at 20:07
  • What is your uid property? Is that your incremental value? If so then remove that as it should automatically generate the id for you – chadpeppers Nov 20 '14 at 20:26
  • The code below creates an entity but not the attached fields global $user; $entity_notes_array = array(); $entity_notes_array['uid'] = $user->uid; $entity_notes_array['field_people_reference'] = 1; // Using 1 to test the reference $entity_notes_array['field_signed'] = 1; $entity_notes = entity_create('people_notes', $entity_notes_array); $entity_notes->save(); – Mark Parrish Nov 20 '14 at 20:26
0

Below is the final solution. Both questions have been answered. I finally figured out why the script was not firing. I was having the VBO on the entity (people_notes) and not the referenced entity (people). Because the people_notes was being created with the action there was nothing being passed to the action. Once I switched the VBO to people it fired successfully and created the people_notes entity.

Also @chadpeppers originally suggested entity_metadata_wrapper but that did not work due to not have a setter function defined for the uid field. I defined a setter function and then entity_metadatawrapper worked correctly.

global $user;
$entity_notes = entity_create('people_notes', array());
try {
    $wrapper = entity_metadata_wrapper('people_notes', $entity_notes);
    $wrapper->uid->set($user->uid);
    $wrapper->field_people_reference->set($entity->PeopleId);
    $wrapper->field_signed->set(1);
    $wrapper->save();
       drupal_set_message(t('Signature Scrubbed!'));
       watchdog('Signed Petition', 'Signed Petition action fired!');
} catch (EntityMetadataWrapperException $exc) {
    watchdog(
      'Signature Scrubbed Action',  
      'See '  . __FUNCTION__ . '() <pre>' .  $exc->getTraceAsString() . '</pre>', 
       NULL, WATCHDOG_ERROR
    );
}

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.