You are here

function alpha_pagination_migrate_option in Views Alpha Pagination 7.2

Migrates an option for a view handler.

Parameters

string|string[] $type: The handler type(s) to migrate options for.

string $from: The old option name.

$to: The new option name.

callable $callback: A callback function to process the old value prior to saving the new value.

1 call to alpha_pagination_migrate_option()
alpha_pagination_update_7201 in ./alpha_pagination.install
Convert "pre_letter_path" option to "paginate_link_path".

File

./alpha_pagination.module, line 75
Module hooks and alters for the Views Alpha Pagination module.

Code

function alpha_pagination_migrate_option($type, $from, $to, callable $callback) {
  alpha_pagination_process_views_handler($type, function (\views_handler $handler) use ($from, $to, $callback) {
    $old_value = NULL;
    if (isset($handler->options[$from])) {
      $old_value = $handler->options[$from];
    }
    $display_id = $handler->view->display[$handler->view->current_display]->id;
    $new_value = $old_value;
    $new_value = call_user_func_array($callback, [
      $new_value,
      $handler,
    ]);

    // Indicate whether the view should be saved based on if the value changed.
    $save = $old_value !== $new_value;
    if ($save) {

      // Retrieve the handler item.
      $type = $handler->handler_type;
      $id = $handler->real_field ?: $handler->field;
      $item = $handler->view
        ->get_item($display_id, $type, $id);

      // Set the new value.
      $item[$to] = $new_value;

      // Remove the old value.
      unset($item[$from]);

      // Set the item.
      $handler->view
        ->set_item($display_id, $type, $id, $item);

      // Display notification of conversion.
      drupal_set_message(t('[@view_name:@display_id] - Converted option "@from" to "@to"', [
        '@view_name' => $handler->view->name,
        '@display_id' => $display_id,
        '@from' => $from,
        '@to' => $to,
      ]));
    }
    return $save;
  });
}