trinion_mrp-1.0.x-dev/trinion_mrp.module

trinion_mrp.module
<?php
/**
 * Implements hook_theme().
 */
function trinion_mrp_theme() {
  $theme = [
    'proizvodstvenniy_kalendar' => [
      'variables' => [
        'days' => [],
      ],
      'template' => 'proizvodstvenniy_kalendar',
    ],
  ];

  return $theme;
}

function trinion_mrp_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if (preg_match('/^node_(.*)_form$/U', $form_id, $match)) {
    if (in_array($match[1], ['zapis_mrp', 'mrp_specifikaciya', 'mps'])) {
      $form['title']['widget']['0']['value']['#default_value'] = Drupal::service('trinion_tp.helper')->getNextDocumentNumber($match[1]);
    }
  }

  if ($form_id == 'node_zapis_mrp_form') {
    $form['field_mrp_record_periods']['#access'] = FALSE;
    $form['actions']['submit']['#submit'][] = 'trinion_mrp_node_zapis_mrp_form_submit';
  }
  if (isset($form['field_mrp_lot_size'], $form['field_mrp_lotsize_value'])) {
    $form['field_mrp_lotsize_value']['widget'][0]['value']['#states'] = [
      'visible' => [
        ':input[name="field_mrp_lot_size"]' => ['value' => '2'],
        ],
    ];
  }
}

/**
 * Implements hook_entity_access().
 */
function trinion_mrp_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
  $bundle = $entity->bundle();
  if ($operation == 'view') {
    if ($bundle == 'zapis_mrp')
      return \Drupal\Core\Access\AccessResult::forbiddenIf(!$account->hasPermission('trinion_mrp zapis mrp'));
    if ($bundle == 'mrp_specifikaciya')
      return \Drupal\Core\Access\AccessResult::forbiddenIf(!$account->hasPermission('trinion_mrp specificatiya mrp'));
    if ($bundle == 'mrp_zakaz_na_proizvodstvo')
      return \Drupal\Core\Access\AccessResult::forbiddenIf(!$account->hasPermission('trinion_mrp zakaz_na_proizvodstvo'));
    if ($bundle == 'mps')
      return \Drupal\Core\Access\AccessResult::forbiddenIf(!$account->hasPermission('trinion_mrp mps'));
    if ($bundle == 'mrp_rabochiy_centr')
      return \Drupal\Core\Access\AccessResult::forbiddenIf(!$account->hasPermission('trinion_mrp rabochiy_centr'));
    elseif (in_array($bundle, ['stroka_zapisi_mrp', 'mrp_scenario']))
      return \Drupal\Core\Access\AccessResult::forbidden();
  }
}

/**
 * Implements hook_entity_presave().
 */
function trinion_mrp_entity_presave(\Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->getEntityType()->id() == 'node' && $entity->bundle() == 'zapis_mrp')
    $entity->title = Drupal::service('trinion_tp.helper')->getNextDocumentNumber('zapis_mrp');
}

function trinion_mrp_node_zapis_mrp_form_submit($form, \Drupal\Core\Form\FormState $form_state) {
  $form_state->setRedirect('entity.node.edit_form', ['node' => $form_state->get('nid')]);
}

function trinion_mrp_preprocess_page(&$variables) {
  $route_match = Drupal::routeMatch();
  $route_name = $route_match->getRouteName();
  if ($route_name == 'entity.node.edit_form') {
    $node = $route_match->getParameter('node');
    if ($node && in_array($node->bundle(), ['mps', ])) {
      $variables['#attached']['library'][] =  'trinion_tp/tp';
    }
  }
  elseif ($route_name == 'node.add') {
    $node_type = $route_match->getParameter('node_type');
    if ($node_type && in_array($node_type->id(), ['mps', ])) {
      $variables['#attached']['library'][] =  'trinion_tp/tp';
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function trinion_mrp_preprocess_field(&$variables) {
  if ($variables['element']['#field_name'] == 'field_mrp_table') {
    foreach ($variables['items'] as $key => $item) {
      $record = json_decode($item['content']['#context']['value'], 1);
      $variables['items'][$key]['content'] = \Drupal\trinion_mrp\Form\MrpRecordCreatorForm::tableElement($record);
    }
  }
}

/**
 * Implements hook_node_view().
 */
function trinion_mrp_node_view(array &$build, \Drupal\node\NodeInterface $node, $display, $view_mode) {
  $bundle = $node->bundle();
  if ($bundle == 'zapis_mrp') {
    $view = \Drupal\views\Views::getView('base_spisok_sviazannih_documentov');
    $view->setDisplay('block_1');
    $docs = Drupal::service('trinion_mrp.helper')->getMrpRalatedOrders($node, 'zakaz_postavschiku');
    $view->setArguments([implode('+', $docs)]);
    $build['spisok_sviazannih_puo'] = $view->render();
    /** @var \Drupal\views\ViewExecutable $view */
    $build['spisok_sviazannih_puo']['#view']->header = [];
    $build['kolichestvo_sviazannih_puo'] = [
      '#markup' => $build['spisok_sviazannih_puo']['#view']->total_rows,
    ];
    $total_puo = 0;
    foreach ($build['spisok_sviazannih_puo']['#view']->result as $pro) {
      $total_puo += $pro->_entity->get('field_tp_itogo')->getString();
    }
    $build['summa_sviazannih_puo'] = [
      '#markup' => $total_puo,
    ];

    $view = \Drupal\views\Views::getView('base_spisok_sviazannih_documentov');
    $view->setDisplay('block_1');
    $docs = Drupal::service('trinion_mrp.helper')->getMrpRalatedOrders($node, 'mrp_zakaz_na_proizvodstvo');
    $view->setArguments([implode('+', $docs)]);
    $build['spisok_sviazannih_pro'] = $view->render();
    $build['spisok_sviazannih_pro']['#view']->header = [];
    $build['kolichestvo_sviazannih_pro'] = [
      '#markup' => $build['spisok_sviazannih_pro']['#view']->total_rows,
    ];
  }
  if (in_array($bundle, ['mrp_zakaz_na_proizvodstvo', ])) {
    $docs = Drupal::service('trinion_base.related_docs')->getRalatedDocs($node);
    $view = \Drupal\views\Views::getView('base_spisok_sviazannih_documentov');
    $view->setDisplay('block_1');
    $view->setArguments([implode('+', $docs)]);
    $build['spisok_sviazannih_documentov'] = $view->render();
  }
}

/**
 * Implements hook_entity_load().
 */
function trinion_mrp_entity_load(array $entities, $entity_type_id)
{
  if ($entity_type_id == 'node') {
    foreach ($entities as $entity) {
      $bundle = $entity->bundle();
      if ($bundle == 'tp_stroka_dokumenta_uit') {
        if ($periods = $entity->get('field_tp_periods')->getString())
          $entity->periods = json_decode($periods, TRUE);
      }
      if (in_array($bundle, ['mrp_zakaz_na_proizvodstvo', 'zapis_mrp', ])) {
        $entity->node_title = t('@document № @num from @date', [
            '@document' => $entity->get('type')->first()->entity->label(),
            '@num' => $entity->label(),
            '@date' => date('j.m.Y', strtotime($entity->get('field_tp_data')->getString())),
          ]
        );
      }
      if (in_array($bundle, ['mrp_rabochee_podrazdelenie', 'mrp_rabochiy_centr', 'mrp_scenario', 'mrp_specifikaciya', 'mrp_tehnologicheskaya_karta',])) {
        $entity->node_title = t('@document № @num from @date', [
            '@document' => $entity->get('type')->first()->entity->label(),
            '@num' => $entity->label(),
            '@date' => date('j.m.Y', $entity->get('created')->getString()),
          ]
        );
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc