You are here

function icon_bundle_save in Icon API 8

Same name and namespace in other branches
  1. 7 icon.module \icon_bundle_save()

Save an icon bundle in the {icon_bundle} table.

Parameters

array $bundle: The icon bundle array.

Return value

int|false If the save failed, returns FALSE. If successful, returns SAVED_NEW or SAVED_UPDATED, depending on the drupal_write_record() operation that was performed.

4 calls to icon_bundle_save()
icon_bundle_configure_form_submit in includes/admin.inc
Submit callback for 'icon_bundle_configure_form'.
icon_bundle_disable in ./icon.module
Disable the icon bundle that matches {icon_bundle}.name in the database.
icon_bundle_enable in ./icon.module
Enable the icon bundle that matches {icon_bundle}.name in the database.
icon_provider_import_form_submit in includes/import.inc
Submit callback for 'icon_provider_import_form'.

File

./icon.module, line 548
icon.module Provides icon integration with menu items.

Code

function icon_bundle_save(array $bundle = array()) {
  if (empty($bundle['name'])) {
    return FALSE;
  }

  // Allow extensions to alter the bundle before it's saved.
  \Drupal::moduleHandler()
    ->alter('icon_bundle_save', $bundle);
  $primary_keys = array();
  $record = array(
    'name' => $bundle['name'],
  );
  $existing_bundle = icon_bundle_load($bundle['name']);
  if (!$existing_bundle) {
    $existing_bundle = array();
  }

  // If the existing bundle is from the database, update the record.
  if (!empty($existing_bundle['database'])) {
    $primary_keys[] = 'name';
  }

  // Determine if there are differences between an existing bundle and this one.
  $diff = icon_array_diff_recursive($existing_bundle, $bundle);

  // If the status has changed, update the status field.
  if (isset($diff['status'])) {
    $record['status'] = (int) $bundle['status'];

    // Remove status from $diff to determine if anything else needs to be saved.
    unset($diff['status']);
  }

  // If there are still differences, replace the entire {icon_bundle}.bundle
  // field with the updated bundle array.
  if (!empty($diff)) {
    $record['bundle'] = $bundle;
  }

  // Only write to the database if the record exists and needs to be saved.
  if (isset($record['status']) || isset($record['bundle'])) {
    $status = \Drupal::database()
      ->merge('icon_bundle')
      ->key(array(
      'name' => $record['name'],
    ))
      ->fields(array(
      'bundle' => $record['bundle'],
      'status' => $record['status'],
    ))
      ->execute();
    if ($status) {
      icon_clear_all_caches();
      return $status;
    }
  }
  return FALSE;
}