|
| 1 | +--- |
| 2 | +title: get_child_block_by_path |
| 3 | +editLink: false |
| 4 | +--- |
| 5 | + |
| 6 | +# get_child_block_by_path |
| 7 | + |
| 8 | +## Description |
| 9 | + |
| 10 | +The `get_child_block_by_path()` method finds a child block from an array of existing child blocks based on its path. This method is particularly useful when you need to access and modify a specific child block within a nested child block structure. |
| 11 | + |
| 12 | +### Responsibility |
| 13 | + |
| 14 | +This method recursively searches through an array of child blocks using a path-based approach (e.g., "table/row/cell") to locate a specific child block. It validates the path and throws an exception if the block cannot be found, ensuring type safety and preventing silent failures. |
| 15 | + |
| 16 | +### Arguments |
| 17 | + |
| 18 | +| Parameter | Type | Required | Description | |
| 19 | +|-----------|------|----------|-------------| |
| 20 | +| `$existing_child_blocks` | `Child_Block[]` | Yes | An array of existing child blocks to search in | |
| 21 | +| `$path` | `string` | Yes | A "/" separated path to block (e.g., "table/row/cell" or "table/row/cell/cell-content") | |
| 22 | + |
| 23 | +### Return Value |
| 24 | + |
| 25 | +- **Type**: `Child_Block` |
| 26 | +- **Description**: Returns the matching Child_Block instance if found |
| 27 | +- **Throws**: `\InvalidArgumentException` if the path is invalid or the block cannot be found |
| 28 | + |
| 29 | +## Path Format |
| 30 | + |
| 31 | +The path parameter uses a forward-slash (`/`) separated format to navigate through nested child blocks: |
| 32 | + |
| 33 | +- Each segment represents a child block name |
| 34 | +- The path is traversed from top to bottom |
| 35 | +- Example: `"table/row/cell"` means: |
| 36 | + - Find a child block named "table" |
| 37 | + - Within that, find a child block named "row" |
| 38 | + - Within that, find a child block named "cell" |
| 39 | + - Return that "cell" block |
| 40 | + |
| 41 | +## Pairing with replace_child_block_by_path |
| 42 | + |
| 43 | +This function pairs exceptionally well with `replace_child_block_by_path()`. The typical workflow is: |
| 44 | + |
| 45 | +1. Use `get_child_block_by_path()` to retrieve the existing child block you want to modify |
| 46 | +2. Make your modifications to the retrieved block (e.g., add fields, change configuration) |
| 47 | +3. Use `replace_child_block_by_path()` to replace the original block with your modified version |
| 48 | + |
| 49 | +This pattern allows you to extend child blocks defined in parent classes without having to completely recreate the entire child block hierarchy. |
| 50 | + |
| 51 | +## Examples |
| 52 | + |
| 53 | +### Basic Usage - Retrieving a Child Block |
| 54 | + |
| 55 | +This example shows how to retrieve a child block from a nested structure: |
| 56 | + |
| 57 | +```php |
| 58 | +use Creode_Blocks\Helpers; |
| 59 | + |
| 60 | +protected function child_blocks(): array { |
| 61 | + $existing_child_blocks = parent::child_blocks(); |
| 62 | + |
| 63 | + // Get a specific child block by path |
| 64 | + $cell_block = Helpers::get_child_block_by_path( |
| 65 | + $existing_child_blocks, |
| 66 | + 'table/row/cell' |
| 67 | + ); |
| 68 | + |
| 69 | + // Now you can access the block's properties |
| 70 | + $fields = $cell_block->fields; |
| 71 | + $template = $cell_block->template; |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Extending a Child Block with Additional Fields |
| 76 | + |
| 77 | +This is the most common use case - extending a child block defined in a parent class by adding new fields: |
| 78 | + |
| 79 | +```php |
| 80 | +use Creode_Blocks\Table_Block as Base_Table_Block; |
| 81 | +use Creode_Blocks\Helpers; |
| 82 | + |
| 83 | +class Table extends Base_Table_Block { |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritdoc} |
| 87 | + */ |
| 88 | + protected function name(): string { |
| 89 | + return 'table'; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * {@inheritdoc} |
| 94 | + */ |
| 95 | + protected function label(): string { |
| 96 | + return 'Comparison Table'; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * {@inheritdoc} |
| 101 | + */ |
| 102 | + protected function child_blocks(): array { |
| 103 | + $existing_child_blocks = parent::child_blocks(); |
| 104 | + |
| 105 | + // Get the existing cell-content child block |
| 106 | + $cell_content_child_block = Helpers::get_child_block_by_path( |
| 107 | + $existing_child_blocks, |
| 108 | + 'table/row/cell/cell-content' |
| 109 | + ); |
| 110 | + |
| 111 | + // Get the existing fields array and add the new field to it |
| 112 | + $fields = $cell_content_child_block->fields; |
| 113 | + $fields[] = array( |
| 114 | + 'key' => 'creode_field', |
| 115 | + 'label' => 'Creode Field', |
| 116 | + 'name' => 'creode_field', |
| 117 | + 'type' => 'select', |
| 118 | + 'choices' => array( |
| 119 | + '' => 'hooray', |
| 120 | + '1' => 'woohoo', |
| 121 | + '2' => 'I did it', |
| 122 | + ), |
| 123 | + ); |
| 124 | + $cell_content_child_block->fields = $fields; |
| 125 | + |
| 126 | + // Replace the original block with the modified version |
| 127 | + $replaced_child_blocks = Helpers::replace_child_block_by_path( |
| 128 | + $existing_child_blocks, |
| 129 | + 'table/row/cell/cell-content', |
| 130 | + $cell_content_child_block |
| 131 | + ); |
| 132 | + |
| 133 | + return $replaced_child_blocks; |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### Modifying Multiple Properties |
| 139 | + |
| 140 | +You can modify multiple properties of the retrieved child block: |
| 141 | + |
| 142 | +```php |
| 143 | +use Creode_Blocks\Helpers; |
| 144 | + |
| 145 | +protected function child_blocks(): array { |
| 146 | + $existing_child_blocks = parent::child_blocks(); |
| 147 | + |
| 148 | + $target_block = Helpers::get_child_block_by_path( |
| 149 | + $existing_child_blocks, |
| 150 | + 'section/header' |
| 151 | + ); |
| 152 | + |
| 153 | + // Modify multiple properties |
| 154 | + // Get the existing fields array, modify it, then reassign it |
| 155 | + $fields = $target_block->fields; |
| 156 | + $fields[] = array( |
| 157 | + 'key' => 'new_field', |
| 158 | + 'label' => 'New Field', |
| 159 | + 'name' => 'new_field', |
| 160 | + 'type' => 'text', |
| 161 | + ); |
| 162 | + $target_block->fields = $fields; |
| 163 | + |
| 164 | + $target_block->template = __DIR__ . '/templates/custom-header.php'; |
| 165 | + |
| 166 | + // Replace with modified version |
| 167 | + return Helpers::replace_child_block_by_path( |
| 168 | + $existing_child_blocks, |
| 169 | + 'section/header', |
| 170 | + $target_block |
| 171 | + ); |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +## Error Handling |
| 176 | + |
| 177 | +The method throws `\InvalidArgumentException` in two scenarios: |
| 178 | + |
| 179 | +1. **Invalid path**: If the path is empty or contains no valid segments |
| 180 | +2. **Block not found**: If any segment in the path cannot be matched to an existing child block |
| 181 | + |
| 182 | +```php |
| 183 | +use Creode_Blocks\Helpers; |
| 184 | + |
| 185 | +try { |
| 186 | + $block = Helpers::get_child_block_by_path( |
| 187 | + $existing_child_blocks, |
| 188 | + 'invalid/path/to/block' |
| 189 | + ); |
| 190 | +} catch ( \InvalidArgumentException $e ) { |
| 191 | + // Handle the error - block not found at specified path |
| 192 | + error_log( $e->getMessage() ); |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +## Use Cases |
| 197 | + |
| 198 | +This helper is particularly useful when: |
| 199 | + |
| 200 | +- **Extending parent block classes**: You want to add fields or modify properties of a child block defined in a parent class |
| 201 | +- **Selective modifications**: You only need to modify specific child blocks in a complex hierarchy |
| 202 | +- **Dynamic child block manipulation**: You need to programmatically access and modify nested child blocks |
| 203 | +- **Pairing with replace_child_block_by_path**: You want to retrieve, modify, and replace a child block in one workflow |
| 204 | + |
| 205 | +## Notes |
| 206 | + |
| 207 | +- The path matching is case-sensitive and must exactly match the child block names |
| 208 | +- Only the first matching child block at each level is returned (if multiple child blocks share the same name, only the first one is found) |
| 209 | +- The method validates the entire path before returning, ensuring the block exists at the specified location |
| 210 | +- Always use this method in conjunction with `replace_child_block_by_path()` when you need to persist your modifications |
| 211 | + |
0 commit comments