<?php

namespace Drupal\basket;

use Drupal\views\Views;
use Drupal\views\ViewsData;
use Drupal\Component\Utility\NestedArray;

class viewsUiAddHandler{
	public static function formAlter(&$form, $form_state){
		if(!empty($form['options']['name']['#options'])){
			$view = $form_state->get('view');
			$viewsData = \Drupal::service('views.views_data')->getAll();
			foreach ($form['options']['name']['#options'] as $keyField => $value){
				$fieldInfo = NestedArray::getValue($viewsData, explode('.', $keyField));
				if(!empty($fieldInfo['basket_views']) && !in_array($view->id(), $fieldInfo['basket_views'])){
					unset($form['options']['name']['#options'][$keyField]);
				}
			}
		}
	}
}