# Views Tools

A powerful Drupal 11 module for managing Views displays with advanced export and deletion capabilities.

## Introduction

Views Tools provides essential utilities for Views management, making it easier to work with complex Views configurations. The module offers a comprehensive set of tools for backing up, exporting, and managing Views displays.

## Features

- **Complete View Backup**: Export entire Views as YAML configuration files
- **Display Export**: Export specific displays as new independent Views
- **Bulk Operations**: Perform operations on multiple displays simultaneously
- **Display Management**: Delete displays without affecting the entire View
- **Security**: Proper access control and validation for all operations
- **Modern Architecture**: Built with Drupal 11 best practices and dependency injection

## Requirements

- Drupal 10.x or 11.x
- PHP 8.1 or higher
- Views module (core)
- Views UI module (core)

## Installation

1. install this module using composer
2. Enable the module through the admin interface at `/admin/modules` or via Drush:
   ```bash
   drush en views_tools
   ```
3. Configure permissions at `/admin/people/permissions` under the "Views Tools" section:
   - **Use Views Tools**: Basic access to views tools interface
   - **Export Views**: Permission to export views and displays as YAML or new views
   - **Delete View Displays**: Permission to delete individual displays (excluding default)
   - **Administer Views Tools**: Full administrative access to all views tools functionality

## Permissions

Views Tools provides granular permissions for different operations:

### Use Views Tools
- **Purpose**: Basic access to the Views Tools interface
- **Scope**: Allows viewing the views list and display management pages
- **Security**: Low risk, read-only access

### Export Views
- **Purpose**: Export views and displays as YAML files or new views
- **Scope**: Access to all export functionality including bulk export operations
- **Security**: Medium risk, can access view configurations

### Delete View Displays
- **Purpose**: Delete individual displays from views
- **Scope**: Access to display deletion (default display is always protected)
- **Security**: High risk, destructive operation

### Administer Views Tools
- **Purpose**: Full administrative access to all Views Tools functionality
- **Scope**: Bypasses all other permission checks, full access to all features
- **Security**: Highest risk, complete access

**Note**: Users with "Administer views" permission automatically have access to all Views Tools functionality.

## Usage

### Accessing Views Tools

Navigate to **Structure > Views Tools** (`/admin/structure/views-tools`) to access the main interface.

### Main Features

#### 1. Views List
- View all available Views in your site
- Quick access to display management and export functions
- Access control based on user permissions

#### 2. Display Management
For each View, you can:
- **View Displays**: See all displays within a View
- **Edit**: Direct link to edit individual displays
- **Export as New View**: Create a new View from selected displays
- **Export YAML**: Download display configuration as YAML
- **Delete Display**: Remove displays (except default)

#### 3. Bulk Operations
Select multiple displays and perform:
- **Export to New View**: Create a single new View with selected displays
- **Export to YAML**: Download configuration for selected displays
- **Delete Displays**: Remove multiple displays at once

### Navigation Structure

```
/admin/structure/views-tools
├── View List (all Views)
├── /view/{view_id} (individual View displays)
├── /export/{view_id} (full View YAML export)
├── /export/{view_id}/display/{display_id} (display as new View)
├── /exportyaml/{view_id}/display/{display_id} (display YAML export)
└── /delete/{view_id}/display/{display_id}/confirm (delete confirmation)
```

## Technical Details

### Architecture

This module follows modern Drupal development practices:

- **Object-oriented Controllers**: Clean separation of concerns
- **Dependency Injection**: Proper service container usage
- **Form API**: Modern form handling with validation
- **Entity API**: Uses Views entities instead of direct database queries
- **Security**: Input validation and access control throughout

### Services Used

- `entity_type.manager`: For loading and managing View entities
- `renderer`: For rendering UI components
- `form_builder`: For form generation and handling
- `config.factory`: For configuration management
- `messenger`: For user feedback and notifications

### Security Features

- **Access Control**: Respects Views permissions
- **Input Validation**: All user inputs are validated
- **CSRF Protection**: Forms include proper token validation
- **Error Handling**: Graceful handling of edge cases

### Testing

The module includes comprehensive test coverage:

#### Functional Tests
- **ViewsToolsLoadTest**: Basic page loading and access
- **ViewsToolsUninstallTest**: Module uninstallation
- **ViewsToolsComprehensiveTest**: Complete functionality testing

#### Unit Tests
- **ViewsToolsTest**: Core functionality validation

Run tests with:
```bash
phpunit web/modules/custom/views_tools/tests/
```

## API Documentation

### ViewsTools Class

Static utility class providing core functionality:

#### Methods

```php
// Delete displays from a View
ViewsTools::deleteDisplay(View $view, $display_ids): bool

// Export displays as a new View
ViewsTools::exportDisplaysAsView(View $view, $display_ids): View

// Export displays to YAML
ViewsTools::exportDisplaysToYaml(View $view, $display_ids): Response
```

### Controller Routes

- `views_tools.list`: Main Views listing
- `views_tools.view`: Individual View displays
- `views_tools.export`: Full View export
- `views_tools.display_export`: Display as new View
- `views_tools.display_export_yaml`: Display YAML export
- `views_tools.display_delete_confirm`: Delete confirmation

## Development

### File Structure

```
views_tools/
├── views_tools.info.yml              # Module metadata
├── views_tools.module                # Hook implementations
├── views_tools.routing.yml           # Route definitions
├── views_tools.links.menu.yml        # Menu links
├── composer.json                     # Composer configuration
├── src/
│   ├── Controller/
│   │   └── ViewsToolsController.php  # Main controller
│   ├── Form/
│   │   ├── BulkOperationForm.php     # Bulk operations form
│   │   └── DeleteDisplayConfirm.php  # Delete confirmation form
│   └── ViewsTools.php                # Utility class
└── tests/
    └── src/
        ├── Functional/               # Functional tests
        └── Unit/                     # Unit tests
```

### Contributing

Bug reports and feature requests should be submitted through the issue queue.

## Maintainers

Current maintainers:
- abhishek (visabhishek@gmail.com)

## License

This project is licensed under the GPL-2.0-or-later license.

To use this goto Structure >> Views Tools.

TODO :
We do have planned for another set of features to be implemented.
1: Editing views display directly though database.
2: Editing only selected views display without worrying about override options.


INSTALLATION
------------

 * Install as you would normally install a contributed Drupal module. See:
   https://drupal.org/documentation/install/modules-themes/modules-8
   for further information.


CONFIGURATION
-------------

 * Configure module on Structure » Views Tools configration form.


MAINTAINERS
-----------

Current maintainers:
 * Sandeep Reddy (sandeepreddyg) - https://www.drupal.org/u/sandeepreddyg
 * Abhishek Vishwakarma (visabhishek) - https://www.drupal.org/u/visabhishek
