php 用于预先填充实体引用字段的丑陋代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 用于预先填充实体引用字段的丑陋代码相关的知识,希望对你有一定的参考价值。

  // When we have pre-fill values try to pre-fill the form.
  if (isset($_REQUEST['pre_fill'])) {
    $pre_fill_values = $_REQUEST['pre_fill'];

    // Detect whether we're editing an inline_entity_form widget.
    $is_inline_entity_form_widget = isset($form_state['inline_entity_form']);
    // Only alter the widget for the Standard Product Display content type..
    $is_standard_product_display = isset($element['#bundle']) && ($element['#bundle'] === 'standard_product_display');

    if ($is_inline_entity_form_widget && $is_standard_product_display
      && $element['#field_name'] === 'field_product' && !empty($pre_fill_values) && isset($pre_fill_values['field_product'])) {
      // In the "invisible" form, find out the part that really helps us here.
      // More info can be found on the following link:
      // https://www.drupal.org/project/inline_entity_form/issues/1757410#comment-6426220.
      foreach ($form_state['inline_entity_form'] as $key_state => $form_state_item) {
        if ($form_state_item['settings']['column'] === 'product_id') {
          break;
        }
      }

      $entity = commerce_product_load($pre_fill_values['field_product']);

      // Set the element values.
      $element['entities'][0] = [];
      $element['entities'][0]['#entity'] = $entity;
      $element['entities'][0]['#needs_save'] = FALSE;
      $element['entities'][0]['#weight'] = 0;
      $element['entities'][0]['delta'] = [
        '#type' => 'weight',
        '#default_value' => 0,
        '#attributes' => ['class' => [0 => ' ief-entity-delta']],
      ];

      $element['entities'][0]['actions'] = [
        '#type' => 'container',
        '#attributes' => ['class' => [0 => ' ief-entity-operations']],
        'ief_entity_edit' => [
          '#type' => 'submit',
          '#value' => t('Edit'),
          '#name' => 'ief-' . $key_state . '-entity-edit-' . 0,
          '#limit_validation_errors' => [],
          '#ajax' => [
            'callback' => 'inline_entity_form_get_element',
            'wrapper' => 'inline-entity-form-' . $key_state,
          ],
          '#submit' => ['inline_entity_form_open_row_form'],
          '#ief_row_delta' => 0,
          '#ief_row_form' => 'edit',
        ],
        'ief_entity_remove' => [
          '#type' => 'submit',
          '#value' => t('Remove'),
          '#name' => 'ief-' . $key_state . '-entity-remove-' . 0,
          '#limit_validation_errors' => [],
          '#ajax' => [
            'callback' => 'inline_entity_form_get_element',
            'wrapper' => 'inline-entity-form-' . $key_state,
          ],
          '#submit' => ['inline_entity_form_open_row_form'],
          '#ief_row_delta' => 0,
          '#ief_row_form' => 'remove',
        ],
      ];

      // Modify the invisible part.
      $form_state['inline_entity_form'][$key_state]['entities'][0] = [
        'entity' => $entity,
        'weight' => 0,
        'form' => NULL,
        'needs_save' => FALSE,
      ];
    }
  }

以上是关于php 用于预先填充实体引用字段的丑陋代码的主要内容,如果未能解决你的问题,请参考以下文章

多行颤动文本字段占据所有灵活空间,右填充丑陋

如何使用另一个 MySQL 表中的现有数据填充预先选择的下拉字段?

css 防止chrome中输入字段的丑陋自动填充背景颜色。

在 facebook iOS 中预先填充评论字段

Firefox 密码字段已预先填充

表单未预先填充Angular 8