diff --git a/core/modules/grid/grid.admin.inc b/core/modules/grid/grid.admin.inc new file mode 100644 index 0000000..22d87d8 --- /dev/null +++ b/core/modules/grid/grid.admin.inc @@ -0,0 +1,67 @@ +render(); +} + +/** + * Page callback: Presents the grid editing form. + * + * @see grid_menu() + */ +function grid_page_edit(Grid $grid) { + drupal_set_title(t('Edit grid @label', array('@label' => $grid->label())), PASS_THROUGH); + return entity_get_form($grid); +} + +/** + * Page callback: Provides the new grid addition form. + * + * @see grid_menu() + */ +function grid_page_add() { + $grid = entity_create('grid', array()); + return entity_get_form($grid); +} + +/** + * Page callback: Form constructor for grid deletion confirmation form. + * + * @see grid_menu() + */ +function grid_delete_confirm($form, &$form_state, Grid $grid) { + // Always provide entity id in the same form key as in the entity edit form. + $form['id'] = array('#type' => 'value', '#value' => $grid->id()); + $form_state['grid'] = $grid; + return confirm_form($form, + t('Are you sure you want to remove the grid %title?', array('%title' => $grid->label())), + 'admin/structure/grids', + t('This action cannot be undone.'), + t('Delete'), + t('Cancel') + ); +} + +/** + * Form submission handler for grid_delete_confirm(). + */ +function grid_delete_confirm_submit($form, &$form_state) { + $grid = $form_state['grid']; + $grid->delete(); + drupal_set_message(t('Grid %label has been deleted.', array('%label' => $grid->label()))); + watchdog('grid', 'Grid %label has been deleted.', array('%label' => $grid->label()), WATCHDOG_NOTICE); + $form_state['redirect'] = 'admin/structure/grids'; +} diff --git a/core/modules/grid/lib/Drupal/grid/GridFormController.php b/core/modules/grid/lib/Drupal/grid/GridFormController.php new file mode 100644 index 0000000..96763d7 --- /dev/null +++ b/core/modules/grid/lib/Drupal/grid/GridFormController.php @@ -0,0 +1,100 @@ +type)) { + $grid->type = 'equal_column'; + $grid->options = array(); + } + $grid->options = $this->getPlugin()->prepareOptions($grid->options); + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::form(). + */ + public function form(array $form, array &$form_state, EntityInterface $grid) { + $form['label'] = array( + '#type' => 'textfield', + '#title' => t('Label'), + '#maxlength' => 255, + '#default_value' => $grid->label(), + '#required' => TRUE, + ); + $form['id'] = array( + '#type' => 'machine_name', + '#default_value' => $grid->id(), + '#machine_name' => array( + 'exists' => 'grid_load', + 'source' => array('label'), + ), + '#disabled' => !$grid->isNew(), + ); + $form['type'] = array( + '#type' => 'value', + '#value' => $grid->type, + ); + $form['options'] = $this->getPlugin()->form($form, $form_state, $grid->options); + + return parent::form($form, $form_state, $grid); + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::validate(). + */ + public function validate(array $form, array &$form_state) { + $this->getPlugin()->validate($form, $form_state['values']['options']); + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::actions(). + */ + protected function actions(array $form, array &$form_state) { + // Only includes a Save action for the entity, no direct Delete button. + return array( + 'submit' => array( + '#value' => t('Save'), + '#validate' => array( + array($this, 'validate'), + ), + '#submit' => array( + array($this, 'submit'), + array($this, 'save'), + ), + ), + ); + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::save(). + */ + public function save(array $form, array &$form_state) { + $grid = $this->getEntity($form_state); + $grid->save(); + + watchdog('grid', 'Grid @label saved.', array('@label' => $grid->label()), WATCHDOG_NOTICE); + drupal_set_message(t('Grid %label saved.', array('%label' => $grid->label()))); + + $form_state['redirect'] = 'admin/structure/grids'; + } + +} diff --git a/core/modules/grid/grid.module b/core/modules/grid/grid.module index 7d99dc3..6063c7f 100644 --- a/core/modules/grid/grid.module +++ b/core/modules/grid/grid.module @@ -8,6 +8,65 @@ use Drupal\grid\Grid; /** + * Implements hook_menu(). + */ +function grid_menu() { + $items = array(); + $items['admin/structure/grids'] = array( + 'title' => 'Grids', + 'description' => 'Manage list of grids which can be used with layouts.', + 'page callback' => 'grid_page_list', + 'access callback' => 'user_access', + 'access arguments' => array('administer grids'), + 'file' => 'grid.admin.inc', + ); + $items['admin/structure/grids/add'] = array( + 'title' => 'Add grid', + 'page callback' => 'grid_page_add', + 'access callback' => 'user_access', + 'access arguments' => array('administer grids'), + 'type' => MENU_LOCAL_ACTION, + 'file' => 'grid.admin.inc', + ); + $items['admin/structure/grids/manage/%grid'] = array( + 'title' => 'Edit grid', + 'page callback' => 'grid_page_edit', + 'page arguments' => array(4), + 'access callback' => 'user_access', + 'access arguments' => array('administer grids'), + 'type' => MENU_CALLBACK, + 'file' => 'grid.admin.inc', + ); + $items['admin/structure/grids/manage/%grid/edit'] = array( + 'title' => 'Edit', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => -10, + ); + $items['admin/structure/grids/grid/%grid/delete'] = array( + 'title' => 'Delete', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('grid_delete_confirm', 4), + 'access callback' => 'user_access', + 'access arguments' => array('administer grids'), + 'type' => MENU_LOCAL_TASK, + 'file' => 'grid.admin.inc', + ); + return $items; +} + +/** + * Implements hook_permission(). + */ +function grid_permission() { + return array( + 'administer grids' => array( + 'title' => t('Administer grids'), + 'description' => t('Administer grids created with the grid module.'), + ), + ); +} + +/** * Implements hook_entity_info(). */ function grid_entity_info() { @@ -15,6 +74,12 @@ function grid_entity_info() { 'label' => t('Grid'), 'entity class' => 'Drupal\grid\Grid', 'controller class' => 'Drupal\Core\Config\Entity\ConfigStorageController', + 'form controller class' => array( + 'default' => 'Drupal\grid\GridFormController', + ), + 'list controller class' => 'Drupal\Core\Config\Entity\ConfigEntityListController', + 'list path' => 'admin/structure/grids', + 'uri callback' => 'grid_uri', 'config prefix' => 'grid', 'entity keys' => array( 'id' => 'id', @@ -24,3 +89,28 @@ function grid_entity_info() { ); return $types; } + +/** + * Entity URI callback. + * + * @param Drupal\grid\Grid $grid + * Grid configuration entity instance. + * + * @return array + * Entity URI information. + */ +function grid_uri(Grid $grid) { + return array( + 'path' => 'admin/structure/grids/manage/' . $grid->id(), + ); +} + +/** + * Look up one grid setup based on machine name. + * + * @return Drupal\grid\Grid + * Grid configuration entity instance. + */ +function grid_load($id) { + return entity_load('grid', $id); +} diff --git a/core/modules/grid/lib/Drupal/grid/Plugin/grid/grid/EqualColumn.php b/core/modules/grid/lib/Drupal/grid/Plugin/grid/grid/EqualColumn.php index a465c2c..881723c 100644 --- a/core/modules/grid/lib/Drupal/grid/Plugin/grid/grid/EqualColumn.php +++ b/core/modules/grid/lib/Drupal/grid/Plugin/grid/grid/EqualColumn.php @@ -78,6 +78,83 @@ public function __construct($id, $units, $width, $columns, $padding_width, $gutt } /** + * Prepare grid entity. + */ + public function prepareOptions(array $options) { + if (empty($options['width'])) { + // Set some defaults for the user if this is a new grid. + $options['units'] = '%'; + $options['width'] = 100; + $options['columns'] = 12; + $options['padding_width'] = 1.5; + $options['gutter_width'] = 2; + } + return $options; + } + + /** + * Form elements for grid editing. + */ + public function form(array $form, array &$form_state, array $options) { + // Master grid configuration. + $form['unit'] = array( + '#type' => 'radios', + '#title' => t('Grid units'), + '#options' => array('%' => t('Percentages'), 'em' => t('Em-based'), 'px' => t('Pixel based')), + '#default_value' => $options['unit'], + ); + $form['width'] = array( + '#type' => 'textfield', + '#title' => t('Grid width'), + '#description' => t('Width of the grid in unit set above. For example 960 (pixels) or 100 (percent).'), + '#default_value' => $options['width'], + ); + + // Grid detail configuration. + $form['columns'] = array( + '#type' => 'textfield', + '#title' => t('Number of grid columns'), + '#default_value' => $options['columns'], + ); + $form['padding_width'] = array( + '#type' => 'textfield', + '#title' => t('Column padding'), + '#description' => t('Column padding in unit set above. For example 10 (pixels) or 1.5 (percent). Enter 0 for no padding.'), + '#default_value' => $options['padding_width'], + ); + + $form['gutter_width'] = array( + '#type' => 'textfield', + '#title' => t('Gutter width'), + '#description' => t('Gutter width in unit set above. For example 20 (pixels) or 1.5 (percent). Enter 0 for no padding.'), + '#default_value' => $options['gutter_width'], + ); + return $form; + } + + /** + * Form validation callback. + */ + public function validate(array $form, array &$options_state) { + if ((intval($options_state['width']) != $options_state['width']) || $options_state['width'] == 0) { + // Width should be a positive integer. + form_set_error('columns', t('The width should be a positive number.')); + } + if ((intval($options_state['columns']) != $options_state['columns']) || $options_state['columns'] == 0) { + // Columns should be a positive integer. + form_set_error('columns', t('The number of columns should be a positive number.')); + } + if (!is_numeric($options_state['padding_width'])) { + // Padding can be float as well (eg. 1.5 for 1.5% for fluid grids). + form_set_error('padding_width', t('The column padding should be a number. Enter 0 (zero) for no padding.' . $options_state['padding_width'])); + } + if (!is_numeric($options_state['gutter_width'])) { + // Gutter can be float too (eg. 1.5 for 1.5% for fluid grids). + form_set_error('gutter_width', t('The gutter width should be a number. Enter 0 (zero) for no gutter.')); + } + } + + /** * Implements Drupal\grid\Plugin\GridInterface::getGridCss(). */ public function getGridCss($wrapper_selector = NULL, $col_selector_prefix = NULL, $skip_spacing = FALSE) {