You are here

function entity_embed_embed_button_save in Entity Embed 7.2

Same name and namespace in other branches
  1. 7.3 entity_embed.module \entity_embed_embed_button_save()
  2. 7 entity_embed.module \entity_embed_embed_button_save()

Saves an embed button object to the database.

Parameters

$embed_button: The embed button to save.

1 call to entity_embed_embed_button_save()
IconFileUsageTest::testEmbedButtonIcon in ./entity_embed.test
Tests the embed_button and file usage integration.
1 string reference to 'entity_embed_embed_button_save'
entity_embed_schema in ./entity_embed.install
Implements hook_schema().

File

./entity_embed.module, line 192
Provides a CKEditor plugin and text filter for embedding and rendering entities.

Code

function entity_embed_embed_button_save($embed_button) {
  ctools_include('export');
  $new_button_icon_fid = $embed_button->button_icon_fid;

  // Find the original button settings to see if they have been changed.
  if ($original = entity_embed_embed_button_load($embed_button->name)) {
    $old_button_icon_fid = $original->button_icon_fid;

    // If the button icon has changed, remove file usage from the old icon.
    if (!empty($old_button_icon_fid) && $old_button_icon_fid != $new_button_icon_fid) {
      if ($file = file_load($old_button_icon_fid)) {

        // Mark the old icon file as temporary since it is no longer needed.
        if ($file->status !== 0) {
          $file->status = 0;
          file_save($file);
        }
        file_usage_delete($file, 'entity_embed', $embed_button->name, 1);
      }
    }
  }

  // Add file usage information to the button icon if it is new.
  if ($new_button_icon_fid) {
    if ($file = file_load($new_button_icon_fid)) {

      // Mark the file as permanent so it won't be cleaned up by the system.
      if ($file->status !== FILE_STATUS_PERMANENT) {
        $file->status = FILE_STATUS_PERMANENT;
        file_save($file);
      }
      $usage = file_usage_list($file);

      // Icons can only be used once per button.
      if (empty($usage['entity_embed'][$embed_button->name][1])) {
        file_usage_add($file, 'entity_embed', $embed_button->name, 1);
      }
    }
  }
  if ($embed_button->export_type & EXPORT_IN_DATABASE) {

    // Existing record.
    $update = array(
      'cid',
    );
  }
  else {

    // New record.
    $update = array();
    $embed_button->export_type = EXPORT_IN_DATABASE;
  }

  // Reset the ctools object cache so that the changes are picked up.
  ctools_export_load_object_reset('entity_embed');
  return drupal_write_record('entity_embed', $embed_button, $update);
}