Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP for PDF 2 #11

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/sync/workflows.workflow.editorial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ langcode: en
status: true
dependencies:
config:
- media.type.remote_video
- node.type.landing_page
module:
- content_moderation
Expand Down Expand Up @@ -62,6 +63,8 @@ type_settings:
to: published
weight: 1
entity_types:
media:
- remote_video
node:
- landing_page
default_moderation_state: draft
51 changes: 51 additions & 0 deletions web/modules/custom/drupal_site_core/drupal_site_core.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,54 @@
* @file
* Contains drupal_site_core.module.
*/
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeInterface;
use Drupal\media\Entity\Media;

final class EntityManager {
public static $changed = [];

public static $updating = false;

public static function checkAtomicity(EntityInterface $entity): bool {
$element = $entity->bundle() . $entity->id();
if (in_array($element, self::$changed)) {
return true;
}
self::$changed []= $element;
return false;
}

public static function checkEntity(EntityInterface $entity) {
if (self::checkAtomicity($entity)) {
return;
}
if ($entity->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') {
self::$updating = TRUE;

$entity->set('moderation_state', 'draft');
$entity->setNewRevision(TRUE);
$entity->save();

self::$updating = FALSE;
}
}
}
}
}

function drupal_site_core_entity_presave(EntityInterface $entity) {
EntityManager::checkEntity($entity);
}


function hook_site_core_entity_presave(EntityInterface $entity) {
// Update the entity's entry in a fictional table of this type of entity.
die('hoooked');
}

30 changes: 30 additions & 0 deletions web/modules/custom/magazine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## INTRODUCTION

The Magazine module is a DESCRIBE_THE_MODULE_HERE.

The primary use case for this module is:

- Use case #1
- Use case #2
- Use case #3

## REQUIREMENTS

DESCRIBE_MODULE_DEPENDENCIES_HERE

## INSTALLATION

Install as you would normally install a contributed Drupal module.
See: https://www.drupal.org/node/895232 for further information.

## CONFIGURATION
- Configuration step #1
- Configuration step #2
- Configuration step #3

## MAINTAINERS

Current maintainers for Drupal 10:

- FIRST_NAME LAST_NAME (NICKNAME) - https://www.drupal.org/u/NICKNAME

11 changes: 11 additions & 0 deletions web/modules/custom/magazine/custom_permissions.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
content manager:
title: 'Content manager'
description: 'Can move from "Draft" to "Review".'

editor:
title: 'Editor'
description: 'Can approve content.'

chief editor:
title: 'Chief Editor'
description: 'Can publish content.'
7 changes: 7 additions & 0 deletions web/modules/custom/magazine/magazine.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'Magazine'
type: module
description: 'Custom Entity and Multilingual Workflow'
package: Custom
core_version_requirement: ^10 || ^11
dependencies:
- drupal:user
21 changes: 21 additions & 0 deletions web/modules/custom/magazine/magazine.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @file
* Install, update and uninstall functions for the Magazine module.
*/
use Drupal\magazine\Module as Module;

/**
* Implements hook_install().
*/
function magazine_install() {
Module::install();
}

/**
* Implements hook_uninstall().
*/
function magazine_uninstall() {
Module::uninstall();
}
44 changes: 44 additions & 0 deletions web/modules/custom/magazine/magazine.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* @file
* Primary module hooks for Magazine module.
*/
declare(strict_types=1);

namespace Drupal\magazine;

final class Module {

public static function createRoles(): void {
//your data array
$data = array('id' => 'client', 'label' => 'Client');
//creating your role
$role = \Drupal\user\Entity\Role::create($data);
//saving your role
$role->save();
}


/**
* install
*
* Performs module instalation.
*
* @return void
*/
public static function install(): void {
// install module
}

/**
* uninstall
*
* Performs module uninstall.
*
* @return void
*/
public static function uninstall(): void {
// uninstall module
}
}
60 changes: 60 additions & 0 deletions web/modules/custom/telemetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## Telemetry Module

This telemetry module is a functional test both to validate technical knowledge and to evaluate the possibilities of features offered by the most current versions of Drupal.

The primary use case for this module is:

- Receive information about execution, installation, etc. quickly and clearly, without the need to access complex panels and systems, using everyday tools;
- Carefully monitor processes that require attention - such as those that may eventually generate additional costs, such as installation, build and/or inadvertent use;
- Store messages sent via telemetry locally and/or remotely;

## REQUIREMENTS

- Drupal 10 or newer;
- DDEV 1.23.4 or newer;
- Composer version 2.7.7 or newer;
- PHP version 8.3.10 or newer;

## INSTALLATION

Install as you would normally install a contributed Drupal module.
See: https://www.drupal.org/node/895232 for further information.


## MAINTAINERS

- John Murowaniecki 🜏 ( _jmurowaniecki_ · aka `0xD3C0de`);


## Contribute

### Getting started

First download and configure the project following the steps below:

```bash
git clone [email protected]:jmurowaniecki/zoocha.git drupal-site
# to acquire the project
```


Inside project directory execute the following commands:
```bash
ddev composer install
# to install project dependencies

ddev import-db --file db.sql.gz
# to import the database
```

Finally you're able to execute the project executing the command `ddev start`

> For better comprehension check this recording containing the first steps:
> [![asciicast](https://asciinema.org/a/gHIIVv3X6amNdcdThfc6ISj9D.svg)](https://asciinema.org/a/gHIIVv3X6amNdcdThfc6ISj9D)


### Creating the base

I used `ddev drush…` to create the base module and form as documented in the recording below. More information can be obtained by looking at the commits made.

[![asciicast](https://asciinema.org/a/z2DJTC3R9TqgYiiHQiWzj7Mlw.svg)](https://asciinema.org/a/z2DJTC3R9TqgYiiHQiWzj7Mlw)
124 changes: 124 additions & 0 deletions web/modules/custom/telemetry/src/Form/TelemetryForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Drupal\telemetry\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\telemetry\Module as Module;
use Drupal\Core\Database\Database;

/**
* Provides a Telemetry form.
*/
final class TelemetryForm extends FormBase {

private $connection;

/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'telemetry_telemetry';
}

/**
* {@inheritdoc}
*/
public function __construct() {
$this->connection = Database::getConnection();
}

/**
* getFields
*
* Should return form fields processed, merged with the provided ones and
* extracted taking the list of parameters.
*
* @param array $form Form fields to append.
* @return array Form fields processed.
*/
private function getFields(array $form = []): array {
return array_merge($form, Module::extractFields(
Module::TABLE_FIELDS,
['message', 'message_type'],
function ($translate) {
return $this->t($translate);
}));
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$query = $this->connection->select('telemetry', 't')
->fields('t', ['message', 'message_type'])
->orderBy('id', 'DESC')
->range(0, 5);
$restoring = $query->execute()->fetchAll();
$elements = sizeof($restoring);
foreach ($restoring as $result) {
$this->messenger()->{$result->message_type}($result->message);
}

Module::telemetry('Status', $this, 'Accessing method *'.__METHOD__.'*.');

$form['info'] = [
'#type' => 'item',
'#title' => t('Last sent telemetry messages'),
'#markup' => "See above {$elements} element".($elements > 1 ? 's' : '').' sent previously.',
];

$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Send'),
],
];
return $this->getFields($form);
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
// @todo Validate the form here.
// Example:
// @code
// if (mb_strlen($form_state->getValue('message')) < 10) {
// $form_state->setErrorByName(
// 'message',
// $this->t('Message should be at least 10 characters.'),
// );
// }
// @endcode
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
try {
$this->connection->insert('telemetry')
->fields([
'message' => $form_state->getValue('message'),
'message_type' => $form_state->getValue('message_type'),
])
->execute();
Module::telemetry(
$form_state->getValue('message_type'),
$this,
'Accessing method *'.__METHOD__.'*. ',
$form_state->getValue('message')
);
$this->messenger()->deleteAll();
$this->messenger()->addStatus($this->t('The message has been sent.'));
} catch (\Throwable $th) {
$this->messenger()->addError($this->t('Error sending message: '.$th->getMessage()));
}
$form_state->setRedirect('<front>');
}

}
Loading