You are here

public function NodeGalleryBehaviorHandler::NodeGalleryRelationshipCrud in Node Gallery 7

Create, update or delete Node Gallery relationships based on field values.

3 calls to NodeGalleryBehaviorHandler::NodeGalleryRelationshipCrud()
NodeGalleryBehaviorHandler::delete in plugins/entityreference/behavior/NodeGalleryBehaviorHandler.class.php
Implements EntityReference_BehaviorHandler_Abstract::Delete().
NodeGalleryBehaviorHandler::insert in plugins/entityreference/behavior/NodeGalleryBehaviorHandler.class.php
Act before inserting an entity reference field.
NodeGalleryBehaviorHandler::update in plugins/entityreference/behavior/NodeGalleryBehaviorHandler.class.php
Act before updating an entity reference field.

File

plugins/entityreference/behavior/NodeGalleryBehaviorHandler.class.php, line 60
Node Gallery EntityReference behavior plugin.

Class

NodeGalleryBehaviorHandler
Node Gallery behavior handler.

Code

public function NodeGalleryRelationshipCrud($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $diff = $this
    ->galleriesGetDiff($entity_type, $entity, $field, $instance, $langcode, $items);
  if (!$diff) {
    return;
  }
  $diff += array(
    'insert' => array(),
    'delete' => array(),
  );

  // Delete first, so we don't trigger cardinality errors.
  if ($diff['delete']) {
    entity_delete_multiple('node_gallery_relationship', array_keys($diff['delete']));
    foreach (array_values($diff['delete']) as $ngid) {
      if (node_gallery_api_get_cover_nid($ngid) == $entity->nid) {
        node_gallery_api_reset_cover_item($ngid);
      }
      node_gallery_api_clear_gallery_caches($ngid);
      node_gallery_api_update_image_counts($ngid);
    }
  }
  if (!empty($diff['insert'])) {
    $relationship_type = node_gallery_api_get_relationship_type(NULL, $entity->type);
    if (!empty($relationship_type)) {
      foreach ($diff['insert'] as $ngid) {
        $r = new NodeGalleryRelationship();
        $r->relationship_type = $relationship_type->id;
        $r->nid = $entity->nid;
        $r->ngid = $ngid;
        $r->weight = NODE_GALLERY_DEFAULT_WEIGHT;
        $r
          ->save();
      }
    }
  }
}