“#”속성이란 무엇입니까?


22

Ajax 프레임 워크 문서를 읽는 동안 나는 그 #ajax속성에 대해 언급했다 . 코드를 파고 들면 해시 기호가있는 다른 속성이 있다는 것을 알고 있습니다. 해시 표시는 무엇을 의미합니까? 이 속성들은 무엇에 관한 것입니까?


이 페이지의 어딘가에 "파운드 기호"를 추가하고 싶을 것입니다. "해시"가 너무 유용한 결과를 얻지 못했을 때이를 알아 내기 위해 사용한 검색어이기 때문입니다.
Max Starkenburg

답변:


23

이것은 일반적 으로 렌더 배열 과 관련이 있으며 AJAX 또는 양식 API에만 국한되지는 않습니다 (양식 API는 렌더 배열을 독점적으로 사용하여 내용을 작성하지만).

간단히 말해, #이름 앞에 렌더 배열의 배열 키는 렌더 배열의 자식으로 표시되고 그 뒤에 자체적으로 (재귀 적으로) 렌더링됩니다.

# 그 앞의 필요에 따라 사용하는 렌더링 어레이에 대한 메타 데이터 / 변수로 간주되며, 그 자체가 표현되지 않는다.

렌더 배열 문서 (위에 링크 됨)는 실제로 이것을 간결하게 멋지게 넣었습니다.

렌더 배열은 렌더링 방식 (#type과 같은 속성)에 대한 힌트와 함께 데이터 (아마도 중첩)를 제공하는 전형적인 Drupal 구조 배열입니다.

#키 '힌트'전항 관해 이야기하고 있는지, 비있는 #키가 중첩 데이터이다.

해당 페이지에 읽기 권한을 부여하는 것이 좋습니다. 전체 렌더 배열에 대한 미스터리를 잘 수행하고 코드 예제를 제공합니다.

Theming the page docs에도 유용한 작은 설명 / 코드 예제 가 있습니다.

렌더 어레이는 Drupal (폼, 테마, 일반 마크 업 등)의 모든 곳에서 사용되므로 이에 대해 잘 알고 있으면 향후 Drupal 개발에 많은 도움이됩니다.


잘 때려 줘
chrisjlee

4

양식 API는 모든 속성 앞에 #을 사용하여 속성과 자식 요소를 구분합니다. 다음 코드에서 $form['choice_wrapper']['choice']하위 요소 인 반면 $form['choice_wrapper']['#tree']속성입니다.

  // Add a wrapper for the choices and more button.
  $form['choice_wrapper'] = array(
    '#tree' => FALSE, 
    '#weight' => -4, 
    '#prefix' => '<div class="clearfix" id="poll-choice-wrapper">', 
    '#suffix' => '</div>',
  );

  // Container for just the poll choices.
  $form['choice_wrapper']['choice'] = array(
    '#prefix' => '<div id="poll-choices">', 
    '#suffix' => '</div>', 
    '#theme' => 'poll_choices',
  );

이러한 모든 특성은 Form API 참조 서에 나열되어 있습니다. 많은 속성이 있지만 렌더링, 유효성 검사 및 제출에 관한 것입니다.

속성에 접두사를 사용하는 이유는 자식 요소에서 속성을 신속하게 필터링 할 수 있기 때문에 다음 코드가 포함 된 drupal_render ()같이 렌더링해야 할 때 유용 합니다.

  // Get the children of the element, sorted by weight.
  $children = element_children($elements, TRUE);

  // Initialize this element's #children, unless a #pre_render callback already
  // preset #children.
  if (!isset($elements['#children'])) {
    $elements['#children'] = '';
  }
  // Call the element's #theme function if it is set. Then any children of the
  // element have to be rendered there.
  if (isset($elements['#theme'])) {
    $elements['#children'] = theme($elements['#theme'], $elements);
  }
  // If #theme was not set and the element has children, render them now.
  // This is the same process as drupal_render_children() but is inlined
  // for speed.
  if ($elements['#children'] == '') {
    foreach ($children as $key) {
      $elements['#children'] .= drupal_render($elements[$key]);
    }
  }

element_children () 을 보면 속성을 필터링하는 코드가 다음과 같습니다.

  // Filter out properties from the element, leaving only children.
  $children = array();
  $sortable = FALSE;
  foreach ($elements as $key => $value) {
    if ($key === '' || $key[0] !== '#') {
      $children[$key] = $value;
      if (is_array($value) && isset($value['#weight'])) {
        $sortable = TRUE;
      }
    }
  }
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.