Skip to content

Instantly share code, notes, and snippets.

@balintbrews
Last active December 16, 2015 19:09
Show Gist options
  • Save balintbrews/5482712 to your computer and use it in GitHub Desktop.
Save balintbrews/5482712 to your computer and use it in GitHub Desktop.
Screenshot: http://i.balintk.com/98cb1d9e7092e055.png. Proof of concept for a Drupal module that provides a simplified user interface for editing a specific view. This way customers are able to make small adjustments without having to face the Views UI.
<?php
/**
* @file
* Provides a minimal UI for editing a specific view.
*/
/**
* Implements hook_menu().
*/
function views_minimal_ui_menu() {
$items = array();
$items['views-minimal-ui'] = array(
'title' => 'Edit view',
'page callback' => 'drupal_get_form',
'page arguments' => array('views_minimal_ui_testing_form'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Minimal UI form.
*/
function views_minimal_ui_testing_form($form, &$form_state) {
// The machine name of the view that we are going to edit is `nodes`.
// Load the view.
$view = views_get_view('nodes', TRUE);
// Select a display.
$view->set_display('default');
$form = array();
$form['view_title'] = array(
'#title' => t('Title'),
'#type' => 'textfield',
'#default_value' => $view->display_handler->get_option('title'),
);
// A filter on content type is already added to the view. In this example we
// only allow the view to be filtered either on Articles or Basic pages.
$filters = $view->display_handler->get_option('filters');
$form['filter'] = array(
'#title' => t('Filter on content type'),
'#type' => 'select',
'#options' => array(
'article' => t('Article'),
'page' => t('Basic page'),
),
'#default_value' => reset($filters['type']['value']),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Minimal UI form submit.
*/
function views_minimal_ui_testing_form_submit($form, &$form_state) {
// Load the view object.
$view = views_get_view('nodes', TRUE);
// Select a display.
$view->set_display('default');
// Set the new title.
$view->display_handler->set_option('title', $form_state['values']['view_title']);
// Set the new filter.
$filters = $view->display_handler->get_option('filters');
$filters['type']['value'] = array($form_state['values']['filter'] => $form_state['values']['filter']);
$view->display_handler->set_option('filters', $filters);
// Save the view object.
$view->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment