我建立了一个自定義欄位,以允许使用者根据帐戶中附加的欄位来設置其隱私設置。
我想获取使用者實體中可用欄位的列表,以便建立一个表選擇表單,使用者可以在其中根据帐戶型別選擇每个欄位的權限。
如何在自定義欄位中執行此操作?
這是我的货币代碼:
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')
];
}
}
最新回復
- 2020-5-201 #
- 2020-5-202 #
//1.Getting specific 'Entity type' manager storage $User = \Drupal::entityTypeManager()->getStorage('user'); //2. load a specific user entity by id // we suppose that we want to get current connected user entity $user_id = \Drupal::currentUser(); $UserEntity = $User->load($user_id); // 3-use this for debugging prupose to display all the entity fields // your field should display here kint($UserEntity->toArray()); // 4.using information collected by kint to pritnt a field using // $UserEntity->{field_name}->{property} // {field_name} = first lvel in array //{property} = exsit in the nested array 2nd level echo $UserEntity->mail->value; echo $UserEntity->name->value; echo $UserEntity->roles->target_id;
您可以使用以下代碼获取使用者實體欄位定義列表: