You are here

function content_browser_form_alter in Content Browser 8

Implements hook_form_alter().

File

./content_browser.module, line 31
Contains logic for content_browser.

Code

function content_browser_form_alter(&$form, FormStateInterface &$form_state) {
  if (isset($form['#form_id'])) {
    if ($form['#form_id'] == 'entity_browser_browse_content_form' || $form['#form_id'] == 'entity_browser_browse_content_iframe_form') {

      // Attach our library.
      $form['#attached']['library'][] = 'content_browser/view';

      // Wrap actions so we can float them at the bottom of the browser.
      $form['actions_wrap'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'content-browser-actions',
          ],
        ],
        0 => $form['widget']['actions'],
      ];
      unset($form['widget']['actions']);

      // Add a class for generic styling.
      $form['#attributes']['class'][] = 'content-browser-form';
    }
    elseif (isset($form['#id']) && $form['#id'] == 'views-exposed-form-content-browser-entity-browser') {

      /** @var \Drupal\views\ViewExecutable $view */
      $view = $form_state
        ->get('view');
      if ($view instanceof ViewExecutable && isset($view->argument['type'])) {

        /** @var \Drupal\node\Plugin\views\argument\Type $type */
        $type = $view->argument['type'];
        $value = $type
          ->getValue();
        if (!is_null($value) && !$type
          ->isException($value)) {
          $types = explode('+', $value);
          $types[] = 'All';
          foreach ($form['type']['#options'] as $name => $option) {
            if (!in_array($name, $types, TRUE)) {
              unset($form['type']['#options'][$name]);
            }
          }
        }
      }
    }
  }
}