View source
<?php
namespace Drupal\field_menu\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Form\FormStateInterface;
class MenuItemId extends FieldItemBase {
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'menu_title' => [
'type' => 'text',
'size' => 'tiny',
'not null' => FALSE,
],
'menu_item_key' => [
'type' => 'text',
'size' => 'tiny',
'not null' => FALSE,
],
'max_depth' => [
'type' => 'int',
'unsigned' => FALSE,
'size' => 'small',
'not null' => FALSE,
],
'include_root' => [
'type' => 'int',
'unsigned' => FALSE,
'size' => 'tiny',
'not null' => FALSE,
],
],
];
}
public function isEmpty() {
$value = $this
->get('menu_item_key')
->getValue();
return $value === NULL || $value === '';
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['menu_title'] = DataDefinition::create('string')
->setLabel(t('Title'));
$properties['menu_item_key'] = DataDefinition::create('string')
->setLabel('');
$properties['include_root'] = DataDefinition::create('integer')
->setLabel(t('Include root'));
$properties['max_depth'] = DataDefinition::create('integer')
->setLabel(t('Max depth'));
return $properties;
}
public static function defaultFieldSettings() {
return [
'menu_type_checkbox' => [],
] + parent::defaultFieldSettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$menu_options = menu_ui_get_menus();
$default_value = !empty($this
->getSetting('menu_type_checkbox')) ? $this
->getSetting('menu_type_checkbox') : [];
$element['menu_type_checkbox'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Available menus'),
'#options' => $menu_options,
'#default_value' => $default_value,
'#description' => $this
->t('Select menu to show on select. Leave empty to show all.'),
];
return $element;
}
}