View source
<?php
namespace Drupal\bulk_update_fields\Form;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BulkUpdateExcludeForm extends ConfigFormBase {
private $entityFieldManager;
private $entityTypeBundleInfo;
private $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfo $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('entity_field.manager'));
}
protected function getEditableConfigNames() {
return [
'bulk_update_fields.settings',
];
}
public function getFormId() {
return 'bulk_update_exclude_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('bulk_update_fields.settings');
$excluded_base_fields = [
'nid',
'uuid',
'vid',
'type',
'revision_uid',
'title',
'menu_link',
'status',
'uid',
'default_langcode',
'revision_timestamp',
'revision_log',
'created',
'changed',
'pass',
'name',
'mail',
'init',
];
$options = [];
$bundles = $this
->getFields('node');
foreach ($bundles as $bundle) {
foreach ($bundle as $field) {
if (!in_array($field
->getName(), $excluded_base_fields) && !isset($options[$field
->getName()])) {
$options[$field
->getName()]['field_name'] = $field
->getLabel() . ' (' . $field
->getName() . ')';
}
}
}
$header = [
'field_name' => $this
->t('Field Name'),
];
$form['table'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#default_value' => $config
->get('exclude') ? $config
->get('exclude') : '',
'#empty' => $this
->t('No fields found'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
protected function getFields($entity_type_id) {
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
foreach ($bundles as $k => $bundle) {
$fields[$k] = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $k);
}
return $fields;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('bulk_update_fields.settings')
->set('exclude', $form_state
->getValue('table'))
->save();
}
}