I have created a custom field to allow users to set their privacy settings based on the fields attached into their account.
I would like to get the list of fields available in the user entity so that I can create a table select form where the users can select the permission per field based on account type.
How to do it in a custom field?
Here's my curreny code:
namespace Drupal\privacy_settings\Plugin\Field\FieldType;
/**
* @FieldType(
* id = "privacy_settings",
* label = @Translation("Privacy Settings"),
* description = @Translation("Privacy Settings"),
* default_widget = "privacy_settings_widget",
* default_formatter = "privacy_settings_formatter_default"
* )
*/
class PrivacySettingsField extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('@operation access', ['@operation' => 'Privacy Settings']));
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
'not null' => TRUE,
],
],
];
}
/**
* {@inheritdoc}
*/
public function setValue($values, $notify = TRUE) {
if (isset($values['value']['table']['rows'])) {
$values['value'] = serialize($values['value']['table']['rows']);
}
parent::setValue($values, $notify);
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
/**
* Return the list of operations available.
*
* @return array
* The list of operations.
*/
public function getOperations() {
return [
t('Account Type 1'),
t('Account Type 2'),
t('Account Type 3')
];
}
}