You are here

function _cacheflush_advanced_form in CacheFlush 8

Form alter callback for add/edit cacheflush entity.

2 calls to _cacheflush_advanced_form()
cacheflush_advanced_form_cacheflush_add_form_alter in modules/cacheflush_advanced/cacheflush_advanced.module
Implements hook_form_FORM_ID_alter().
cacheflush_advanced_form_cacheflush_edit_form_alter in modules/cacheflush_advanced/cacheflush_advanced.module
Implements hook_form_FORM_ID_alter().

File

modules/cacheflush_advanced/cacheflush_advanced.module, line 42
Cacheflush Advanced module.

Code

function _cacheflush_advanced_form(&$form, &$form_state, $op = 'add') {
  $form['vertical_tabs_advance']['cacheflush_advanced_table'] = [
    '#type' => 'table',
    '#header' => [
      t('Cache ID'),
      t('Service'),
      t('Operations'),
    ],
    '#attributes' => [
      'id' => 'cacheflush-advanced-settings-table',
    ],
  ];

  // Add new row button.
  $form['vertical_tabs_advance']['advance_add'] = [
    '#name' => 'advance_add',
    '#type' => 'button',
    '#value' => t('Add another row'),
    '#ajax' => [
      'callback' => '_cacheflush_advanced_form_callback',
      'wrapper' => 'cacheflush-advanced-settings-table',
      'method' => 'replace',
      'effect' => 'fade',
    ],
  ];
  $form['vertical_tabs_advance']['note'] = [
    '#type' => 'item',
    '#title' => t('Note'),
    '#weight' => -10,
    '#description' => t('Create custom preset to clear cache by "cid".') . '<ul><li>' . t('<b>Cache ID ($cid)</b>: The cache ID to delete.') . '</li><li>' . t('<b>Bin</b>: Cache service.') . '</li></ul>',
  ];
  $trigger = $form_state
    ->getTriggeringElement();
  if (isset($trigger)) {
    $table_elements = $form_state
      ->getValues()['vertical_tabs_advance']['cacheflush_advanced_table'];

    // Check if Remove row and remove.
    if (strpos($trigger['#name'], 'advanced_remove_') === 0) {
      $remove_id = explode('advanced_remove_', $trigger['#name'])[1];
      unset($table_elements[$remove_id]);
    }

    // ReCreat table.
    if (isset($table_elements)) {
      foreach ($table_elements as $key => $value) {
        _cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], $key, $value['cid'], $value['table']);
      }
    }

    // Check if Add row and add 1 more.
    if ($trigger['#name'] == 'advance_add') {
      $id = 1;
      if (isset($table_elements)) {
        $id = max(array_keys($table_elements)) + 1;
      }
      _cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], $id);
    }
  }
  else {
    if ($op == 'add') {
      _cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], 0);
    }
    else {
      $data = $form_state
        ->getStorage()['data'];
      if (isset($data['advanced'])) {
        $container = \Drupal::getContainer();
        $bins = $container
          ->getParameter('cache_bins');
        foreach ($data['advanced']['functions'] as $key => $value) {
          _cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], $key, $value['#params'][2], $bins[$value['#params'][0]]);
        }
      }
      else {
        _cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], 0);
      }
    }
  }
}