<commit_before># Adding new batch operations for pieces

Apostrophe includes several handy batch operations on multiple pieces, such as "trash," "rescue from trash," "publish," "unpublish," "tag" and "remove tag." But sometimes you'll want a batch feature of your own.

Adding a custom batch action is not hard. For simplicity, let's pretend that the `publish` batch operation doesn't already exist, and we want to add it.

## Server side steps

### The `addBatchOperations` option

Set the addBatchOperations option, either for all `apostrophe-pieces` or for a specific subclass of pieces. Decide now, and follow the instructions below accordingly. These examples assume you want to do it for all kinds of pieces.

```javascript
// lib/modules/apostrophe-pieces/index.js
module.exports = {
  addBatchOperations: [
    {
      name: 'publish',
      label: 'Publish',
      // If the user's current filter only shows published stuff,
      // don't offer this operation
      unlessFilter: {
        published: true
      },
      // If the schema is missing this field, don't offer this operation
      requiredField: 'publish'
    }
  ]
}
```

> The `requiredField` option makes sure the `publish` field hasn't been removed from the schema altogether for this type of piece. It has nothing to do with requiring the user to do something.
> The `unlessFilter` option removes this batch operation when a particular filter in the manage view has a particular value. Apostrophe's boolean filters can be `true`, `false` or `null` (not filtering right now).

### The `publish` route

Next, add a route with the same name for your pieces module. (Remember, Apostrophe can already publish things, but we're demonstrating how to create that feature from scratch.)

```javascript
// lib/modules/apostrophe-pieces/index.js
module.exports = {
  addBatchOperations: [<commit_msg>Fix typo error in adding batch operations documentation (#314)<commit_after># Adding new batch operations for pieces

Apostrophe includes several handy batch operations on multiple pieces, such as "trash," "rescue from trash," "publish," "unpublish," "tag" and "remove tag." But sometimes you'll want a batch feature of your own.

Adding a custom batch action is not hard. For simplicity, let's pretend that the `publish` batch operation doesn't already exist, and we want to add it.

## Server side steps

### The `addBatchOperations` option

Set the addBatchOperations option, either for all `apostrophe-pieces` or for a specific subclass of pieces. Decide now, and follow the instructions below accordingly. These examples assume you want to do it for all kinds of pieces.

```javascript
// lib/modules/apostrophe-pieces/index.js
module.exports = {
  addBatchOperations: [
    {
      name: 'publish',
      label: 'Publish',
      // If the user's current filter only shows published stuff,
      // don't offer this operation
      unlessFilter: {
        published: true
      },
      // If the schema is missing this field, don't offer this operation
      requiredField: 'published'
    }
  ]
}
```

> The `requiredField` option makes sure the `publish` field hasn't been removed from the schema altogether for this type of piece. It has nothing to do with requiring the user to do something.
> The `unlessFilter` option removes this batch operation when a particular filter in the manage view has a particular value. Apostrophe's boolean filters can be `true`, `false` or `null` (not filtering right now).

### The `publish` route

Next, add a route with the same name for your pieces module. (Remember, Apostrophe can already publish things, but we're demonstrating how to create that feature from scratch.)

```javascript
// lib/modules/apostrophe-pieces/index.js
module.exports = {
  addBatchOperations: [
{'fraction_non_alphanumeric': 0.0784676354029062, 'fraction_numerical': 0.0007926023778071334, 'mean_word_length': 3.9232769830949286, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1795071', 'n_tokens_mistral': 999, 'n_tokens_neox': 939, 'n_words': 492}