MachineName.php in Machine name 8
File
src/Plugin/Field/FieldWidget/MachineName.php
View source
<?php
namespace Drupal\machine_name\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class MachineName extends WidgetBase {
public static function defaultSettings() {
return array(
'editable' => FALSE,
) + parent::defaultSettings();
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$disabled = FALSE;
$op = $form_state
->getFormObject()
->getOperation();
if ($form_state
->getFormObject()
->getBaseFormId() != 'field_config_form') {
if (!$this
->getSetting('editable') && $op == 'edit') {
$disabled = TRUE;
}
}
$widget = [
'#type' => 'machine_name',
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
'#maxlength' => 64,
'#disabled' => $disabled,
'#machine_name' => array(
'exists' => array(
$this,
'exists',
),
),
];
$element['value'] = $element + $widget;
return $element;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['editable'] = array(
'#type' => 'checkbox',
'#title' => t('Editable'),
'#description' => t('Allows field to be editable in saved entity.'),
'#default_value' => $this
->getSetting('editable'),
);
return $form;
}
public function settingsSummary() {
$summary = [];
if (!empty($this
->getSetting('editable'))) {
$summary[] = t('Editable: @editable', array(
'@editable' => $this
->getSetting('editable') ? 'yes' : 'no',
));
}
return $summary;
}
public function exists($value) {
return FALSE;
}
}
Classes
Name |
Description |
MachineName |
Plugin annotation
@FieldWidget(
id = "machine_name",
label = @Translation("Machine name"),
field_types = {
"machine_name"
},
) |