View source
<?php
namespace Drupal\social_profile_fields\Form;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\field\FieldConfigStorage;
use Drupal\profile\ProfileStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialProfileFieldsFlushForm extends ConfirmFormBase {
protected $profileStorage;
protected $configFactory;
protected $fieldStorage;
public function __construct(ProfileStorage $profiel_storage, ConfigFactory $config_factory, FieldConfigStorage $field_storage) {
$this->profileStorage = $profiel_storage;
$this->configFactory = $config_factory;
$this->fieldStorage = $field_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager')
->getStorage('profile'), $container
->get('config.factory'), $container
->get('entity.manager')
->getStorage('field_config'));
}
public function getFormId() {
return 'social_profile_fields_flush_form';
}
public function getQuestion() {
return $this
->t('Flush profile.');
}
public function getCancelUrl() {
return new Url('social_profile_fields.settings');
}
public function getConfirmText() {
return $this
->t('Yes, continue');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$pids = \Drupal::entityQuery('profile')
->condition('type', 'profile')
->execute();
$fields = $this
->getUnselectedFields();
$batch = [
'title' => t('Flushing profiles.'),
'operations' => [
[
'\\Drupal\\social_profile_fields\\SocialProfileFieldsBatch::performFlush',
[
$pids,
$fields,
],
],
],
'finished' => '\\Drupal\\social_profile_fields\\SocialProfileFieldsBatch::performFlushFinishedCallback',
];
batch_set($batch);
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
public function getDescription() {
$fields = $this
->getUnselectedFields();
$field_string = implode(', ', $fields);
return $this
->t('<strong>WARNING</strong>: Flushing profile data will permanently <strong>remove all data</strong> from the following fields from the database: %fields. The search indexes may also be cleared and will need re-indexing. This <strong>cannot be undone</strong>. Are you sure you want to continue?', [
'%fields' => $field_string,
]);
}
protected function getUnselectedFields() {
$profile_fields = $this->fieldStorage
->loadByProperties([
'entity_type' => 'profile',
'bundle' => 'profile',
]);
$settings = $this->configFactory
->get('social_profile_fields.settings');
$empty = [];
foreach ($profile_fields as $key => $value) {
$setting_id = str_replace('.', '_', $key);
$sval = $settings
->get($setting_id);
if (isset($sval) && $sval == FALSE) {
$empty[] = $value
->getName();
}
if ($setting_id === 'profile_profile_field_profile_address') {
if (isset($sval) && $sval == FALSE) {
$empty[] = 'country';
}
$city_val = $settings
->get('profile_address_field_city');
if (isset($city_val) && $city_val == FALSE) {
$empty[] = 'locality';
}
$address_val = $settings
->get('profile_address_field_address');
if (isset($address_val) && $address_val == FALSE) {
$empty[] = 'addressLine1';
}
$postalcode_val = $settings
->get('profile_address_field_postalcode');
if (isset($postalcode_val) && $postalcode_val == FALSE) {
$empty[] = 'postalCode';
}
}
}
return $empty;
}
}