Skip to content

Commit

Permalink
Merge pull request #175 from acquia-pso/DEG-152
Browse files Browse the repository at this point in the history
DEG-152: Added machine name validation.
  • Loading branch information
omkar-pednekar authored Jun 6, 2021
2 parents 54fc83b + 4872c19 commit 76997f0
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/BaseEntityGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,23 @@ public function updateEntityType(EntityInterface $entity_type, array $data) {
/**
* Function to check data having required fields to create entity.
*
* @param $data
* @param array $data
* Entity data from sheet.
* @param string $type
* Entity type.
*
* @return bool
* Return status based on fields availability.
*/
public function requiredFieldsCheck($data, $type = 'Entity type') {
public function requiredFieldsCheck(array $data, string $type = 'Entity type') {
$missing_fields = [];
foreach ($this->requiredFields as $requiredField) {
if (array_key_exists($requiredField, $data) && $data[$requiredField] == '') {
$missing_fields[] = $requiredField;
}
}
if (!empty($missing_fields)) {
$this->io()->warning("$type can not be created with empty " . implode(', ', $missing_fields) .". Skipping creation...");
$this->io()->warning("$type can not be created with empty " . implode(', ', $missing_fields) . ". Skipping creation...");
$status = FALSE;
}
else {
Expand All @@ -364,4 +367,30 @@ public function requiredFieldsCheck($data, $type = 'Entity type') {
return $status;
}

/**
* Helper function to validate machine name.
*
* @param string $machine_name
* Machine name string to validate.
* @param int $length
* Expected maximum length.
* @param string $separator
* Separator to match in string.
*
* @return bool
* Returns true or false based on matching result.
*/
public function validateMachineName(string $machine_name, int $length = 32, string $separator = '_') {
$result = FALSE;
$pattern = "/^[a-z0-9$separator]+$/";
if (strlen($machine_name) <= $length && preg_match($pattern, $machine_name)) {
$result = TRUE;
}
else {
$message = "The machine-readable name must contain only lowercase letters, numbers, and underscores with maximum length of $length. Skipping bundle creation with machine name $machine_name";
$this->io()->warning($message);
}
return $result;
}

}
3 changes: 3 additions & 0 deletions src/Commands/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ private function getNodeTypeData(array $data) {
if (!$this->requiredFieldsCheck($item, 'Content type')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'])) {
continue;
}
$node = [];
$node['name'] = $item['name'];
$node['type'] = $item['machine_name'];
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/CustomBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ private function getCustomBlockTypeData(array $data) {
if (!$this->requiredFieldsCheck($item, 'Custom block type')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'])) {
continue;
}
$block_content = [];
$block_content['label'] = $item['name'];
$block_content['id'] = $item['machine_name'];
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/ImageStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ private function getImageStyleData(array $styles, array $effects) {
if (!$this->requiredFieldsCheck($item, 'Image style')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'])) {
continue;
}
$image_style = [];
$image_style['label'] = $item['style_name'];
$image_style['name'] = $item['machine_name'];
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ private function getMediaTypeData(array $data) {
if (!$this->requiredFieldsCheck($item, 'Media type')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'])) {
continue;
}
$source_option = $item['settings/notes'];
if (!\in_array($source_option, $source_options)) {
$implode_source_options = \implode(',', $source_options);
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ private function getMenuData(array $data) {
if (!$this->requiredFieldsCheck($item, 'Menu')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'], 32, '-_')) {
continue;
}
$menu = [];
$description = isset($item['description']) ? $item['description'] : $item['name'] . ' menu.';
$menu['id'] = str_replace('_', '-', $item['machine_name']);
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ private function getParagraphTypeData(array $data) {
if (!$this->requiredFieldsCheck($item, 'Paragraph type')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'])) {
continue;
}
$paragraph = [];
$paragraph['label'] = $item['name'];
$paragraph['id'] = $item['machine_name'];
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ private function getUserRoleData(array $data) {
if (!$this->requiredFieldsCheck($item, 'User role')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'])) {
continue;
}
$user_role = [];
$user_role['id'] = $item['machine_name'];
$user_role['label'] = $item['name'];
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/Vocabulary.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ private function getVocabTypeData(array $data) {
if (!$this->requiredFieldsCheck($item, 'Vocabulary')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'])) {
continue;
}
$vocabs = [];
$description = isset($item['description']) ? $item['description'] : $item['name'] . ' vocabulary.';
$vocabs['vid'] = $item['machine_name'];
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ private function getWorkflowTypeData(array $data) {
if (!$this->requiredFieldsCheck($item, 'Workflow')) {
continue;
}
if (!$this->validateMachineName($item['machine_name'])) {
continue;
}
$workflow = [];
$workflow['label'] = $item['label'];
$workflow['id'] = $item['machine_name'];
Expand Down
26 changes: 26 additions & 0 deletions src/Services/GeneralApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ public function generateEntityFields(string $bundle_type, array $fields_data, ar
$bundleVal = '';
$bundle = $field['bundle'];
$field_machine_name = $field['machine_name'];
if (!$this->validateMachineName($field_machine_name)) {
$message = $this->t("The machine-readable name must contain only lowercase letters, numbers, and underscores with maximum length of 32. Skipping bundle creation with machine name @machine_name",
['@machine_name' => $field_machine_name]);
$this->logger->warning($message);
continue;
}
$bundle_name = trim(substr($bundle, 0, strpos($bundle, "(")));
if (array_key_exists($bundle_name, $bundles_data)) {
$bundleVal = $bundles_data[$bundle_name];
Expand Down Expand Up @@ -667,4 +673,24 @@ public function fieldDependencyCheck(array $field_meta, array $field) {
return $field;
}

/**
* Helper function to validate machine name.
*
* @param string $machine_name
* Machine name string to validate.
* @param int $length
* Expected maximum length.
*
* @return bool
* Returns true or false based on matching result.
*/
public function validateMachineName(string $machine_name, int $length = 32) {
$result = FALSE;
$pattern = "/^[a-z0-9_]+$/";
if (strlen($machine_name) <= $length && preg_match($pattern, $machine_name)) {
$result = TRUE;
}
return $result;
}

}

0 comments on commit 76997f0

Please sign in to comment.