diff --git a/.gitignore b/.gitignore index 5c0ee76..b71fcde 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ # Ignore IDE files /.idea +.ddev \ No newline at end of file diff --git a/config/sync/core.extension.yml b/config/sync/core.extension.yml index c5a35e6..df50331 100644 --- a/config/sync/core.extension.yml +++ b/config/sync/core.extension.yml @@ -18,6 +18,7 @@ module: contextual: 0 datetime: 0 dblog: 0 + drupal_site_core: 0 dynamic_page_cache: 0 editor: 0 entity_browser: 0 @@ -46,6 +47,7 @@ module: path_alias: 0 search: 0 shortcut: 0 + simple_form: 0 system: 0 taxonomy: 0 text: 0 diff --git a/config/sync/user.role.anonymous.yml b/config/sync/user.role.anonymous.yml index b62cc7d..88e9b27 100644 --- a/config/sync/user.role.anonymous.yml +++ b/config/sync/user.role.anonymous.yml @@ -7,6 +7,7 @@ dependencies: module: - comment - contact + - content_moderation - filter - media - search @@ -23,4 +24,6 @@ permissions: - 'access site-wide contact form' - 'search content' - 'use text format restricted_html' + - 'view any unpublished content' + - 'view latest version' - 'view media' diff --git a/config/sync/user.role.authenticated.yml b/config/sync/user.role.authenticated.yml index ad9009b..3cbd2ee 100644 --- a/config/sync/user.role.authenticated.yml +++ b/config/sync/user.role.authenticated.yml @@ -7,6 +7,7 @@ dependencies: module: - comment - contact + - content_moderation - file - filter - media @@ -29,4 +30,6 @@ permissions: - 'search content' - 'skip comment approval' - 'use text format basic_html' + - 'view any unpublished content' + - 'view latest version' - 'view media' diff --git a/config/sync/workflows.workflow.editorial.yml b/config/sync/workflows.workflow.editorial.yml index 8e7f00e..c761e95 100644 --- a/config/sync/workflows.workflow.editorial.yml +++ b/config/sync/workflows.workflow.editorial.yml @@ -3,6 +3,8 @@ langcode: en status: true dependencies: config: + - media.type.remote_video + - media.type.video - node.type.landing_page module: - content_moderation @@ -62,6 +64,9 @@ type_settings: to: published weight: 1 entity_types: + media: + - remote_video + - video node: - landing_page default_moderation_state: draft diff --git a/web/modules/custom/drupal_site_core/drupal_site.info.yml b/web/modules/custom/drupal_site_core/drupal_site_core.info.yml similarity index 88% rename from web/modules/custom/drupal_site_core/drupal_site.info.yml rename to web/modules/custom/drupal_site_core/drupal_site_core.info.yml index eb262c0..3f67a62 100644 --- a/web/modules/custom/drupal_site_core/drupal_site.info.yml +++ b/web/modules/custom/drupal_site_core/drupal_site_core.info.yml @@ -2,4 +2,4 @@ name: 'Custom core' type: module description: "Base module (default config, general tasks/updates, etc.)." core_version_requirement: '^9.4 || ^10' -package: Custom +package: Assessment diff --git a/web/modules/custom/drupal_site_core/drupal_site_core.module b/web/modules/custom/drupal_site_core/drupal_site_core.module index 3c78d09..bdefb85 100644 --- a/web/modules/custom/drupal_site_core/drupal_site_core.module +++ b/web/modules/custom/drupal_site_core/drupal_site_core.module @@ -1,6 +1,57 @@ getEntityTypeId() == 'media' && $entity->bundle() == 'remote_video') { + $node = \Drupal::routeMatch()->getParameter('node'); + + if ($node && !$node->isDefaultRevision()) { + if ($entity->hasField('moderation_state') && $entity->get('moderation_state')->value !== 'draft') { + $violations = $entity->validate(); + if (count($violations) > 0) { + foreach ($violations as $violation) { + \Drupal::messenger()->addError($violation->getMessage()); + } + return; + } + + if (!\Drupal::currentUser()->hasPermission('edit media')) { + \Drupal::messenger()->addError('You do not have permission to edit this media entity.'); + return; + } + + $is_updating = TRUE; + + $entity->set('moderation_state', 'draft'); + + $entity->setNewRevision(TRUE); + + $entity->setRevisionUserId(\Drupal::currentUser()->id()); + + $entity->setRevisionLogMessage('Set moderation state to draft programmatically.'); + + try { + if ($entity->save()) { + \Drupal::messenger()->addMessage('Set moderation state to draft programmatically.'); + } else { + \Drupal::messenger()->addError('Entity save returned false.'); + } + } catch (\Exception $e) { + \Drupal::messenger()->addError('An error occurred during entity save: ' . $e->getMessage()); + } + + $is_updating = FALSE; + } + } + } +} \ No newline at end of file diff --git a/web/modules/custom/simple_form/simple_form.info.yml b/web/modules/custom/simple_form/simple_form.info.yml new file mode 100644 index 0000000..9321cf6 --- /dev/null +++ b/web/modules/custom/simple_form/simple_form.info.yml @@ -0,0 +1,12 @@ +name: 'Practical Coding Challenges' +description: 'Provides a simple form with two fields: a text +field and a select list. Upon submission, the form should save the data to a +custom table in the database and display a confirmation message.' +package: Assessment +core_version_requirement: ^10 +type: module + +version: '1.0' +datestamp: 1630242476 +dependencies: + - drupal:field \ No newline at end of file diff --git a/web/modules/custom/simple_form/simple_form.links.menu.yml b/web/modules/custom/simple_form/simple_form.links.menu.yml new file mode 100644 index 0000000..5272a85 --- /dev/null +++ b/web/modules/custom/simple_form/simple_form.links.menu.yml @@ -0,0 +1,6 @@ +simple_form.admin_config_form: + title: 'Simple Form' + description: 'Configure the Simple Form.' + parent: system.admin_config_system + route_name: simple_form.form + weight: 100 \ No newline at end of file diff --git a/web/modules/custom/simple_form/simple_form.module b/web/modules/custom/simple_form/simple_form.module new file mode 100644 index 0000000..1aa0d56 --- /dev/null +++ b/web/modules/custom/simple_form/simple_form.module @@ -0,0 +1,38 @@ +schema()->createTable('simple_form_data', [ + 'fields' => [ + 'id' => [ + 'type' => 'serial', + 'not null' => TRUE, + ], + 'text_field' => [ + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ], + 'select_field' => [ + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ], + ], + 'primary key' => ['id'], + ]); +} + +/** + * Implements hook_uninstall(). + */ +function simple_form_uninstall() { + \Drupal::database()->schema()->dropTable('simple_form_data'); +} \ No newline at end of file diff --git a/web/modules/custom/simple_form/simple_form.routing.yml b/web/modules/custom/simple_form/simple_form.routing.yml new file mode 100644 index 0000000..8582217 --- /dev/null +++ b/web/modules/custom/simple_form/simple_form.routing.yml @@ -0,0 +1,7 @@ +simple_form.form: + path: '/admin/simple-form' + defaults: + _form: '\Drupal\simple_form\Form\SimpleForm' + _title: 'Simple Form' + requirements: + _permission: 'access content' diff --git a/web/modules/custom/simple_form/src/Form/SimpleForm.php b/web/modules/custom/simple_form/src/Form/SimpleForm.php new file mode 100644 index 0000000..7c4f714 --- /dev/null +++ b/web/modules/custom/simple_form/src/Form/SimpleForm.php @@ -0,0 +1,85 @@ +select('simple_form_data', 'sfd') + ->fields('sfd', ['text_field', 'select_field']) + ->orderBy('id', 'DESC') + ->range(0, 1); + $result = $query->execute()->fetchAssoc(); + + $default_text = isset($result['text_field']) ? $result['text_field'] : ''; + $default_select = isset($result['select_field']) ? $result['select_field'] : ''; + + $form['text_field'] = [ + '#type' => 'textfield', + '#title' => $this->t('Text Field'), + '#default_value' => $default_text, + '#required' => TRUE, + ]; + + $form['select_field'] = [ + '#type' => 'select', + '#title' => $this->t('Select Field'), + '#options' => [ + 'option_1' => $this->t('Option 1'), + 'option_2' => $this->t('Option 2'), + 'option_3' => $this->t('Option 3'), + ], + '#default_value' => $default_select, + '#required' => TRUE, + ]; + + $form['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Submit'), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $text = $form_state->getValue('text_field'); + $select = $form_state->getValue('select_field'); + + $connection = Database::getConnection(); + $connection->insert('simple_form_data') + ->fields([ + 'text_field' => $text, + 'select_field' => $select, + ]) + ->execute(); + + $this->messenger()->addMessage($this->t('The form has been submitted successfully.')); + } +}