public BundleModerationConfigurationForm::form(array $form, FormStateInterface $form_state)
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- core/modules/content_moderation/src/Form/BundleModerationConfigurationForm.php, line 52
Class
- BundleModerationConfigurationForm
- Form for configuring moderation usage on a given entity bundle.
Namespace
Drupal\content_moderation\Form
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | public function form( array $form , FormStateInterface $form_state ) { /* @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $bundle */ $bundle = $form_state ->getFormObject()->getEntity(); $form [ 'enable_moderation_state' ] = [ '#type' => 'checkbox' , '#title' => $this ->t( 'Enable moderation states.' ), '#description' => $this ->t( 'Content of this type must transition through moderation states in order to be published.' ), '#default_value' => $bundle ->getThirdPartySetting( 'content_moderation' , 'enabled' , FALSE), ]; // Add a special message when moderation is being disabled. if ( $bundle ->getThirdPartySetting( 'content_moderation' , 'enabled' , FALSE)) { $form [ 'enable_moderation_state_note' ] = [ '#type' => 'item' , '#description' => $this ->t( 'After disabling moderation, any existing forward drafts will be accessible via the "Revisions" tab.' ), '#states' => [ 'visible' => [ ':input[name=enable_moderation_state]' => [ 'checked' => FALSE], ], ], ]; } $states = $this ->entityTypeManager->getStorage( 'moderation_state' )->loadMultiple(); $label = function (ModerationState $state ) { return $state ->label(); }; $options_published = array_map ( $label , array_filter ( $states , function (ModerationState $state ) { return $state ->isPublishedState(); })); $options_unpublished = array_map ( $label , array_filter ( $states , function (ModerationState $state ) { return ! $state ->isPublishedState(); })); $form [ 'allowed_moderation_states_unpublished' ] = [ '#type' => 'checkboxes' , '#title' => $this ->t( 'Allowed moderation states (Unpublished)' ), '#description' => $this ->t( 'The allowed unpublished moderation states this content-type can be assigned.' ), '#default_value' => $bundle ->getThirdPartySetting( 'content_moderation' , 'allowed_moderation_states' , array_keys ( $options_unpublished )), '#options' => $options_unpublished , '#required' => TRUE, '#states' => [ 'visible' => [ ':input[name=enable_moderation_state]' => [ 'checked' => TRUE], ], ], ]; $form [ 'allowed_moderation_states_published' ] = [ '#type' => 'checkboxes' , '#title' => $this ->t( 'Allowed moderation states (Published)' ), '#description' => $this ->t( 'The allowed published moderation states this content-type can be assigned.' ), '#default_value' => $bundle ->getThirdPartySetting( 'content_moderation' , 'allowed_moderation_states' , array_keys ( $options_published )), '#options' => $options_published , '#required' => TRUE, '#states' => [ 'visible' => [ ':input[name=enable_moderation_state]' => [ 'checked' => TRUE], ], ], ]; // The key of the array needs to be a user-facing string so we have to fully // render the translatable string to a real string, or else PHP errors on an // object used as an array key. $options = [ $this ->t( 'Unpublished' )->render() => $options_unpublished , $this ->t( 'Published' )->render() => $options_published , ]; $form [ 'default_moderation_state' ] = [ '#type' => 'select' , '#title' => $this ->t( 'Default moderation state' ), '#options' => $options , '#description' => $this ->t( 'Select the moderation state for new content' ), '#default_value' => $bundle ->getThirdPartySetting( 'content_moderation' , 'default_moderation_state' , 'draft' ), '#states' => [ 'visible' => [ ':input[name=enable_moderation_state]' => [ 'checked' => TRUE], ], ], ]; $form [ '#entity_builders' ][] = [ $this , 'formBuilderCallback' ]; return parent::form( $form , $form_state ); } |
Please login to continue.