Skip to content
Snippets Groups Projects
Commit a2d5ad0a authored by Vladimir Roudakov's avatar Vladimir Roudakov
Browse files

Issue #3343609 by vladimiraus, annmarysruthy, _pratik_: Fix the issues reported by phpcs

parent 4bcdf303
Branches 7.x-3.x
Tags 7.x-3.3
1 merge request!10Issue #3343609: 7.x phpcs coding standards
Pipeline #317634 failed
include:
- project: $_GITLAB_TEMPLATES_REPO
ref: $_GITLAB_TEMPLATES_REF
file:
- '/includes/include.drupalci.main-d7.yml'
- '/includes/include.drupalci.variables.yml'
- '/includes/include.drupalci.workflows.yml'
variables:
SKIP_ESLINT: '1'
phpcs:
allow_failure: false
phpstan:
stage: validate
rules:
- if: '$SKIP_PHPSTAN == "1"'
when: never
- when: on_success
artifacts:
when: always
reports:
codequality: phpstan-report.json
script:
- vendor/bin/phpstan analyze --memory-limit=2G --no-interaction --no-progress --error-format=gitlab $_WEB_ROOT/modules/custom | tee phpstan-report.json
...@@ -10,7 +10,7 @@ http://drupal.org/node/70151. ...@@ -10,7 +10,7 @@ http://drupal.org/node/70151.
Configuration options Configuration options
------------- -------------
- Range: The range is set when adding a weight field to an entity. It defines
that fields available weight range. For example, a range of 20 will allow
you to select a weight between -20 and 20.
- Range: The range is set when adding a weight field to an entity. It defines
that fields available weight range. For example, a range of 20 will allow
you to select a weight between -20 and 20.
<?php <?php
// phpcs:disable Drupal.NamingConventions
/**
* Provides a Views field handler for selecting a weight in a specified range.
*/
class views_handler_weight_selector extends views_handler_field { class views_handler_weight_selector extends views_handler_field {
function init(&$view, &$options) { /**
* Initialize the field handler.
*
* @param object $view
* The view object.
* @param array $options
* An array of options.
*/
public function init(&$view, &$options) {
parent::init($view, $options); parent::init($view, $options);
$this->options['weight_selector'] = TRUE; $this->options['weight_selector'] = TRUE;
} }
function option_definition() { /**
* Define options for the field handler.
*
* @return array
* An array of option definitions.
*/
public function option_definition() {
$options = parent::option_definition(); $options = parent::option_definition();
$options['weight_range'] = array( $options['weight_range'] = [
'default' => 20, 'default' => 20,
); ];
return $options; return $options;
} }
function options_form(&$form, &$form_state) { /**
* Build the options form for the field handler.
*
* @param array $form
* An array representing the form.
* @param array $form_state
* An array representing the form state.
*/
public function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state); parent::options_form($form, $form_state);
$form['weight_range'] = array( $form['weight_range'] = [
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Range'), '#title' => t('Range'),
'#description' => t('The range of weights available to select. For '#description' => t('The range of weights available to select. For example, a range of 20 will allow you to select a weight between -20 and 20.'),
example, a range of 20 will allow you to select a weight between -20
and 20.'),
'#default_value' => $this->options['weight_range'], '#default_value' => $this->options['weight_range'],
'#size' => 5, '#size' => 5,
); ];
} }
function render($values) { /**
* Render the weight selector.
*
* @param array $values
* An array of values.
*
* @return string
* The rendered weight selector HTML.
*/
public function render($values) {
return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->'; return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
} }
function access() { /**
* Check access for the weight selector.
*
* @return bool
* TRUE if the user has access to assign node weights; otherwise, FALSE.
*/
public function access() {
return user_access('assign node weight'); return user_access('assign node weight');
} }
function views_form(&$form, &$form_state) { /**
* Build the form for the weight selector.
*
* @param array $form
* An array representing the form.
* @param array $form_state
* An array representing the form state.
*/
public function views_form(&$form, &$form_state) {
// The view is empty, abort. // The view is empty, abort.
if (empty($this->view->result)) { if (empty($this->view->result)) {
$form['#access'] = FALSE; $form['#access'] = FALSE;
return; return;
} }
$form[$this->options['id']] = array( $form[$this->options['id']] = [
'#tree' => TRUE, '#tree' => TRUE,
); ];
$options = _weight_get_options($this->options['weight_range']); $options = _weight_get_options($this->options['weight_range']);
$entities = entity_get_info(); $entities = entity_get_info();
...@@ -67,41 +115,49 @@ class views_handler_weight_selector extends views_handler_field { ...@@ -67,41 +115,49 @@ class views_handler_weight_selector extends views_handler_field {
} }
} }
// At this point, the query has already been run, so we can access the results // At this point, query has already been run, so we can access the results.
foreach ($this->view->result as $row_index => $row) { foreach ($this->view->result as $row_index => $row) {
$entity = entity_load($entity_type, array($row->{$entity_key})); $entity = entity_load($entity_type, [$row->{$entity_key}]);
$form[$this->options['id']][$row_index]['weight'] = array( $form[$this->options['id']][$row_index]['weight'] = [
'#type' => 'select', '#type' => 'select',
'#title' => t('Weight for row @number', array('@number' => $row_index + 1)), '#title' => t('Weight for row @number', ['@number' => $row_index + 1]),
'#title_display' => 'invisible', '#title_display' => 'invisible',
'#options' => $options, '#options' => $options,
'#default_value' => $this->get_value($row), '#default_value' => $this->get_value($row),
'#attributes' => array('class' => array('weight-selector')), '#attributes' => ['class' => ['weight-selector']],
); ];
$form[$this->options['id']][$row_index]['entity'] = array( $form[$this->options['id']][$row_index]['entity'] = [
'#type' => 'value', '#type' => 'value',
'#value' => $entity, '#value' => $entity,
); ];
} }
$form['entity_type'] = array( $form['entity_type'] = [
'#type' => 'value', '#type' => 'value',
'#value' => $entity_type, '#value' => $entity_type,
); ];
$form['views_field'] = array( $form['views_field'] = [
'#type' => 'value', '#type' => 'value',
'#value' => $this->field, '#value' => $this->field,
); ];
$form['#action'] = request_uri(); $form['#action'] = request_uri();
} }
function views_form_submit($form, &$form_state) { /**
* Submit handler for the Views form.
*
* @param array $form
* An array representing the form.
* @param array $form_state
* An array representing the form state.
*/
public function views_form_submit($form, &$form_state) {
$values = $form_state['values']; $values = $form_state['values'];
$field_name = str_replace('_selector', '' , $values['views_field']); $field_name = str_replace('_selector', '', $values['views_field']);
foreach ($values[$values['views_field']] as $value) { foreach ($values[$values['views_field']] as $value) {
$entity = array_pop($value['entity']); $entity = array_pop($value['entity']);
...@@ -110,4 +166,7 @@ class views_handler_weight_selector extends views_handler_field { ...@@ -110,4 +166,7 @@ class views_handler_weight_selector extends views_handler_field {
entity_save($values['entity_type'], $entity); entity_save($values['entity_type'], $entity);
} }
} }
} }
// phpcs:enable
...@@ -12,10 +12,12 @@ function weight_field_views_data($field) { ...@@ -12,10 +12,12 @@ function weight_field_views_data($field) {
$data = field_views_field_default_views_data($field); $data = field_views_field_default_views_data($field);
$field_name = $field['field_name']; $field_name = $field['field_name'];
foreach ($data as $table_name => $table_data) { foreach ($data as $table_data) {
if (isset($table_data[$field_name])) { if (isset($table_data[$field_name])) {
$selector_field = $table_data[$field_name]; $selector_field = $table_data[$field_name];
$title = t('@title Selector', array('@title' => $table_data[$field_name]['title'])); $title = t('@title Selector', [
'@title' => $table_data[$field_name]['title'],
]);
$selector_field['title'] = $title; $selector_field['title'] = $title;
$selector_field['title short'] = $title; $selector_field['title short'] = $title;
$selector_field['field']['handler'] = 'views_handler_weight_selector'; $selector_field['field']['handler'] = 'views_handler_weight_selector';
......
...@@ -9,21 +9,22 @@ ...@@ -9,21 +9,22 @@
* Implements hook_field_schema(). * Implements hook_field_schema().
*/ */
function weight_field_schema($field) { function weight_field_schema($field) {
return array( return [
'columns' => array( 'columns' => [
'value' => array( 'value' => [
'type' => 'int', 'type' => 'int',
'not null' => TRUE, 'not null' => TRUE,
), ],
), ],
'indexes' => array( 'indexes' => [
'value' => array('value'), 'value' => ['value'],
), ],
); ];
} }
/** /**
* Create a Weight field for each enabled content type. * Create a Weight field for each enabled content type.
*
* @see _weight_prepare_weight_field() * @see _weight_prepare_weight_field()
*/ */
function weight_update_7300() { function weight_update_7300() {
...@@ -56,7 +57,7 @@ function weight_update_7301(&$sandbox) { ...@@ -56,7 +57,7 @@ function weight_update_7301(&$sandbox) {
->execute(); ->execute();
$node_ids = []; $node_ids = [];
foreach($weights as $weight) { foreach ($weights as $weight) {
$node_ids[$weight->entity_id] = $weight->entity_id; $node_ids[$weight->entity_id] = $weight->entity_id;
} }
$nodes = node_load_multiple($node_ids); $nodes = node_load_multiple($node_ids);
...@@ -74,6 +75,15 @@ function weight_update_7301(&$sandbox) { ...@@ -74,6 +75,15 @@ function weight_update_7301(&$sandbox) {
} }
} }
/**
* Prepare the weight field for use.
*
* This function clears the field cache and creates a weight field for each
* content type that has weight enabled.
*
* @return string
* The name of the created weight field.
*/
function _weight_prepare_weight_field() { function _weight_prepare_weight_field() {
field_cache_clear(); field_cache_clear();
$field_name = 'field_weight'; $field_name = 'field_weight';
...@@ -87,36 +97,36 @@ function _weight_prepare_weight_field() { ...@@ -87,36 +97,36 @@ function _weight_prepare_weight_field() {
->execute(); ->execute();
// Make sure the field name doesn't exist. // Make sure the field name doesn't exist.
$field = field_read_field($field_name, array('include_inactive' => TRUE)); $field = field_read_field($field_name, ['include_inactive' => TRUE]);
if (!empty($field)) { if (!empty($field)) {
$i = 2; $i = 2;
while ($field = field_read_field($field_name . $i, array('include_inactive' => TRUE))) { while ($field = field_read_field($field_name . $i, ['include_inactive' => TRUE])) {
$i++; $i++;
} }
$field_name = $field_name . $i; $field_name = $field_name . $i;
} }
$field = array( $field = [
'field_name' => $field_name, 'field_name' => $field_name,
'type' => 'weight', 'type' => 'weight',
); ];
field_create_field($field); field_create_field($field);
foreach ($types as $type) { foreach ($types as $type) {
if ($type->weight_enabled && array_key_exists($type->type, $node_types)) { if ($type->weight_enabled && array_key_exists($type->type, $node_types)) {
$instance = array( $instance = [
'field_name' => $field_name, 'field_name' => $field_name,
'entity_type' => 'node', 'entity_type' => 'node',
'label' => t('Weight'), 'label' => t('Weight'),
'bundle' => $type->type, 'bundle' => $type->type,
'settings' => array( 'settings' => [
'range' => $type->weight_range, 'range' => $type->weight_range,
), ],
'widget' => array( 'widget' => [
'type' => 'weight_selector', 'type' => 'weight_selector',
), ],
); ];
field_create_instance($instance); field_create_instance($instance);
} }
} }
......
...@@ -9,30 +9,30 @@ ...@@ -9,30 +9,30 @@
* Implements hook_field_info(). * Implements hook_field_info().
*/ */
function weight_field_info() { function weight_field_info() {
return array( return [
'weight' => array( 'weight' => [
'label' => t('Weight'), 'label' => t('Weight'),
'description' => t('Weight Field'), 'description' => t('Weight Field'),
'instance_settings' => array( 'instance_settings' => [
'range' => 20, 'range' => 20,
), ],
'default_widget' => 'weight_selector', 'default_widget' => 'weight_selector',
'default_formatter' => 'weight_integer', 'default_formatter' => 'weight_integer',
'property_type' => 'integer', 'property_type' => 'integer',
), ],
); ];
} }
/** /**
* Implements hook_field_widget_info(). * Implements hook_field_widget_info().
*/ */
function weight_field_widget_info() { function weight_field_widget_info() {
return array( return [
'weight_selector' => array( 'weight_selector' => [
'label' => t('Weight Selector'), 'label' => t('Weight Selector'),
'field types' => array('weight'), 'field types' => ['weight'],
), ],
); ];
} }
/** /**
...@@ -41,15 +41,15 @@ function weight_field_widget_info() { ...@@ -41,15 +41,15 @@ function weight_field_widget_info() {
function weight_field_widget_settings_form($field, $instance) { function weight_field_widget_settings_form($field, $instance) {
$settings = $instance['widget']['settings']; $settings = $instance['widget']['settings'];
$form['range'] = array( $form['range'] = [
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Range'), '#title' => t('Range'),
'#description' => t('The range of weights available to select. For '#description' => t('The range of weights available to select. For
example, a range of 20 will allow you to select a weight between -20 example, a range of 20 will allow you to select a weight between -20
and 20.'), and 20.'),
'#default_value' => isset($settings['range']) ? $settings['range'] : 20, '#default_value' => $settings['range'] ?? 20,
'#size' => 5, '#size' => 5,
); ];
return $form; return $form;
} }
...@@ -73,14 +73,14 @@ function weight_field_widget_form(&$form, &$form_state, $field, $instance, $lang ...@@ -73,14 +73,14 @@ function weight_field_widget_form(&$form, &$form_state, $field, $instance, $lang
$default_value = $items[$delta]['value']; $default_value = $items[$delta]['value'];
} }
$element += array( $element += [
'#type' => 'select', '#type' => 'select',
'#options' => _weight_get_options($range), '#options' => _weight_get_options($range),
'#default_value' => $default_value, '#default_value' => $default_value,
'#element_validate' => array('weight_selector_validate'), '#element_validate' => ['weight_selector_validate'],
); ];
return array('value' => $element); return ['value' => $element];
} }
/** /**
...@@ -94,22 +94,22 @@ function weight_selector_validate($element, &$form_state) { ...@@ -94,22 +94,22 @@ function weight_selector_validate($element, &$form_state) {
* Implements hook_field_formatter_info(). * Implements hook_field_formatter_info().
*/ */
function weight_field_formatter_info() { function weight_field_formatter_info() {
return array( return [
'weight_integer' => array( 'weight_integer' => [
'label' => t('Default'), 'label' => t('Default'),
'field types' => array('weight'), 'field types' => ['weight'],
), ],
); ];
} }
/** /**
* Implements hook_field_formatter_view(). * Implements hook_field_formatter_view().
*/ */
function weight_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { function weight_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array(); $element = [];
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => $item['value']); $element[$delta] = ['#markup' => $item['value']];
} }
return $element; return $element;
...@@ -131,26 +131,26 @@ function weight_field_is_empty($item, $field) { ...@@ -131,26 +131,26 @@ function weight_field_is_empty($item, $field) {
* Implements hook_permission(). * Implements hook_permission().
*/ */
function weight_permission() { function weight_permission() {
return array( return [
'assign node weight' => array( 'assign node weight' => [
'title' => t('Assign node weights'), 'title' => t('Assign node weights'),
'description' => t('Allow modification of node weights.'), 'description' => t('Allow modification of node weights.'),
), ],
); ];
} }
/** /**
* Implements hook_views_api(). * Implements hook_views_api().
*/ */
function weight_views_api() { function weight_views_api() {
return array( return [
'api' => 3, 'api' => 3,
'path' => drupal_get_path('module', 'weight') . '/views', 'path' => drupal_get_path('module', 'weight') . '/views',
); ];
} }
/** /**
* Implementes hook_preprocess_views_view_table(). * Implements hook_preprocess_views_view_table().
*/ */
function weight_preprocess_views_view_table(&$variables) { function weight_preprocess_views_view_table(&$variables) {
$weight_selector = FALSE; $weight_selector = FALSE;
...@@ -179,7 +179,7 @@ function weight_preprocess_views_view_table(&$variables) { ...@@ -179,7 +179,7 @@ function weight_preprocess_views_view_table(&$variables) {
* Get weight range options. * Get weight range options.
*/ */
function _weight_get_options($range) { function _weight_get_options($range) {
$options = array(); $options = [];
for ($i = -$range; $i <= $range; $i++) { for ($i = -$range; $i <= $range; $i++) {
$options[$i] = $i; $options[$i] = $i;
...@@ -189,7 +189,7 @@ function _weight_get_options($range) { ...@@ -189,7 +189,7 @@ function _weight_get_options($range) {
} }
/** /**
* Implements hook_feeds_processor_targets_alter() * Implements hook_feeds_processor_targets_alter().
*/ */
function weight_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { function weight_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
if ($entity_type == 'node') { if ($entity_type == 'node') {
...@@ -198,14 +198,14 @@ function weight_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_n ...@@ -198,14 +198,14 @@ function weight_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_n
$field_info = field_info_field($field_name); $field_info = field_info_field($field_name);
$type = $field_info['type']; $type = $field_info['type'];
if($type == 'weight'){ if ($type == 'weight') {
$field_name = $fields_instance['field_name']; $field_name = $fields_instance['field_name'];
$targets[$field_name] = array( $targets[$field_name] = [
'name' => $fields_instance['label'], 'name' => $fields_instance['label'],
'callback' => 'weight_feeds_set_target', 'callback' => 'weight_feeds_set_target',
'description' => t('The weight of the node field !field_name.', array('!field_name' => $fields_instance['label'])), 'description' => t('The weight of the node field !field_name.', ['!field_name' => $fields_instance['label']]),
); ];
} }
} }
} }
...@@ -214,21 +214,21 @@ function weight_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_n ...@@ -214,21 +214,21 @@ function weight_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_n
/** /**
* Callback for mapping weights. * Callback for mapping weights.
* *
* @param $source * @param FeedsSource $source
* Field mapper source settings. * Field mapper source settings.
* @param $entity * @param Object $entity
* An entity object, for instance a node object. * An entity object, for instance a node object.
* @param $target * @param array $target
* A string identifying the target on the node. * A string identifying the target on the node.
* @param $value * @param array $values
* The value to populate the target with. * The value to populate the target with.
* @param $mapping * @param array $mapping
* Associative array of the mapping settings from the per mapping * Associative array of the mapping settings from the per mapping
* configuration form. * configuration form.
*/ */
function weight_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) { function weight_feeds_set_target(FeedsSource $source, Object $entity, array $target, array $values, array $mapping) {
if (!is_array($values)) { if (!is_array($values)) {
$values = array($values); $values = [$values];
} }
$weight = 0; $weight = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment