You are here

function eck__entity_type__form_submit in Entity Construction Kit (ECK) 7.3

Same name and namespace in other branches
  1. 7 eck.entity_type.inc \eck__entity_type__form_submit()
  2. 7.2 eck.entity_type.inc \eck__entity_type__form_submit()

Submit handler for adding an entity type.

File

./eck.entity_type.inc, line 202
ENTITY TYPE

Code

function eck__entity_type__form_submit($form, &$state) {
  if ($state['values']['op'] == t("Save")) {

    // This are required so I don't have to do any checks.
    $entity_type = $state['values']['entity_type'];

    // If the entity type is new set up its name and its label
    // create its table, and add the default bundle to the
    // eck_bundle table.
    if ($entity_type->is_new) {

      // Set up name and label.
      $entity_type_name = $state['values']['entity_type_name'];
      $entity_type->name = $entity_type_name;
      $entity_type_label = $state['values']['entity_type_label'];
      $entity_type->label = $entity_type_label;

      // Add the bundle to the table.
      // Process the bundle input from the user.
      if (!empty($state['values']['bundle_name'])) {
        $bundle_name = $state['values']['bundle_name'];
        if (!empty($state['values']['bundle_label'])) {
          $bundle_label = $state['values']['bundle_label'];
        }
        else {
          $bundle_label = ucfirst($bundle_name);
        }
      }
      else {
        $bundle_name = $entity_type_name;
        $bundle_label = $entity_type_label;
      }

      // Lets set up the object and save it to the db.
      $bundle = new Bundle();
      $bundle->entity_type = $entity_type->name;
      $bundle->name = $bundle_name;
      $bundle->label = $bundle_label;
      $bundle
        ->save();
      Bundle::loadAll(NULL, TRUE);
    }

    // Lets handle the default properties.
    eck__default_properties__form_submit($form, $state, $entity_type);
    if ($entity_type->is_new) {
      drupal_set_message(t('The entity type %entity_type has been created.', array(
        '%entity_type' => $entity_type->label,
      )));
    }
    else {
      drupal_set_message(t('The entity type %entity_type has been updated.', array(
        '%entity_type' => $entity_type->label,
      )));
    }
    $entity_type
      ->save();

    // Clear info caches in order to pick up newly created entities.
    EntityType::loadAll(NULL, TRUE);
    drupal_get_schema(NULL, TRUE);
    entity_info_cache_clear();
    variable_set('menu_rebuild_needed', TRUE);
  }
  $state['redirect'] = 'admin/structure/entity-type/' . $entity_type->name;
}