You are here

function eck_entity_type_features_rebuild in Entity Construction Kit (ECK) 7.2

Same name and namespace in other branches
  1. 7.3 eck.features.inc \eck_entity_type_features_rebuild()
  2. 7 eck.features.inc \eck_entity_type_features_rebuild()

Implements hook_features_rebuild().

1 call to eck_entity_type_features_rebuild()
eck_entity_type_features_revert in ./eck.features.inc
Implements hook_features_revert().

File

./eck.features.inc, line 85
Integration with the Features module.

Code

function eck_entity_type_features_rebuild($module) {
  if ($default_entities = features_get_default('eck_entity_type', $module)) {
    foreach ($default_entities as $entity_type_name => $entity_type_info) {

      // Previous versions of ECK would allow deleted entity types to be
      // exported as an empty definition without a machine name. We therefore
      // have to ensure we are actually rebuilding a valid definition.
      if (empty($entity_type_name) || !is_string($entity_type_name)) {
        continue;
      }

      // Load the existing entity type, if one exists.
      $entity_type = EntityType::loadByName($entity_type_name);
      if (empty($entity_type->id)) {
        $entity_type = new EntityType();
      }

      // Look at all of the existing entities properties, and if one exists
      // that does not exist in the code definition, delete it.
      foreach ($entity_type->properties as $property_key => $property) {
        if (!isset($entity_type_info['properties'][$property_key])) {
          $entity_type
            ->removeProperty($property_key);
        }
      }

      // Look at all properties as defined in code, and add or change them
      // Depending on wether they already exist or not.
      foreach ($entity_type_info as $key => $value) {
        if ($key == 'properties') {

          // Loop through the new properties.
          foreach ($entity_type_info['properties'] as $property_key => $property) {

            // If the property already exists, update the behavior.
            if (isset($entity_type->properties[$property_key])) {
              $entity_type
                ->changeBehavior($property_key, $property['behavior']);
            }
            else {

              // Property didn't already exist, so lets create it.
              $entity_type
                ->addProperty($property_key, $property['label'], $property['type'], $property['behavior']);
            }
          }
        }
        else {
          $entity_type->{$key} = $value;
        }
      }
      $entity_type
        ->save();
    }
    eck_clean_up();
  }
}